WebService,老技术了。现在也还在用。
我这边的Android终端调试webservice接口,一开始想看日志,用的方法真笨啊。
装了个WireShark抓包工具在那看收发的报文,看着真费劲。
又改进,让终端打印出日志来看,使用的是ksoap2框架。
if(resultSoapObject == null){
Log.d(TAG+"请求:",httpTransportSE.requestDump);
Log.d(TAG+"应答:",httpTransportSE.responseDump);
}
即可。
但是每次调试都要从新编译下载到机器中?效率还是不高。
最后,还是PostMan工具强大好用。
共三步操作,如下:
第一步:POST地址栏里填入WebService地址
第二步:Header里填入两项内容:
SOAPAction: http://NewCap.com/NewCapecWebService/GetCheckNumber
Content-Type: text/xml;charset=utf-8
第三步,填入Body的内容:
示例如下:内容类型选择RAW
1 6
11.11.11.12
2019-06-14T08:57:39
1
KwEul5zOhWy8lnXdVeDiaw==
1.0
1454236707
至此,全部结束,直接点击Send按钮提交即可。
截图如下:
package com.newcapec.webservice;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.newcapec.utils.AppConfig;
import com.newcapec.utils.CxfUtils;
import org.apache.http.client.HttpResponseException;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CommonWebService {
private static String TAG = "CommonWebService";
// 含有3个线程的线程池
private static final ExecutorService executorService = Executors
.newFixedThreadPool(3);
// 命名空间
//private static final String NAMESPACE = AppConfig.xServxiceNameSpace;
private static final String NAMESPACE = AppConfig.WebServiceNameSpace;
private static final String TestWebService = "http://xx.168.51.xx:xx/websxcexx/xx.exxx.xxxWeb.asmx";
static String sIP = "xx.xxxx.xxxxxx.xxxxxxxx";
static String sID = "1";
static String sAppTypeID = "x";
static String sKey = "xxx";
/**
* @param
* @param methodName WebService的调用方法名
* @param properties WebService的参数
* @param webServiceCallBack 回调接口
*/
public static void callWebService(final String methodName, HashMap properties,
final CommonWebServiceCallRequest webServiceCallBack) {
// 创建HttpTransportSE对象,传递WebService服务器地址
// final HttpTransportSE httpTransportSE = new HttpTransportSE(
// AppConfig.HuaWeiWebService);
final HttpTransportSE httpTransportSE = new HttpTransportSE(
TestWebService,10000);
Element[] header = new Element[1];
header[0] = new Element().createElement(NAMESPACE,
"CredentialSoapHeader");
Element appID = new Element().createElement(NAMESPACE, "ID");
appID.addChild(Node.TEXT, sID);
header[0].addChild(Node.ELEMENT, appID);
Element appTypeID = new Element().createElement(NAMESPACE, "AppTypeID");
appTypeID.addChild(Node.TEXT, sAppTypeID);
header[0].addChild(Node.ELEMENT, appTypeID);
Element iP = new Element().createElement(NAMESPACE, "IP");
iP.addChild(Node.TEXT, sIP);
header[0].addChild(Node.ELEMENT, iP);
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
SimpleDateFormat sdf1 = new SimpleDateFormat(
"yyyyMMddHHmmss", Locale.CHINA);
Date d1 = new Date();
String dateStr1 = sdf1.format(d1);
String dateStr0 = sdf.format(d1);
String[] date1 = dateStr0.split(" ");
String dateStr = date1[0] + "T" + date1[1];
Element timestamp = new Element().createElement(NAMESPACE, "Timestamp");
timestamp.addChild(Node.TEXT, dateStr);
header[0].addChild(Node.ELEMENT, timestamp);
Element random = new Element().createElement(NAMESPACE, "Random");
random.addChild(Node.TEXT, "1");
header[0].addChild(Node.ELEMENT, random);
String shash = null;
byte[] vhash = null;
try {
vhash = CxfUtils.computeHash(sID, dateStr1, "1", sKey);
shash = Base64.encodeToString(vhash, 0, vhash.length, 0);
// LogUtils.i(TAG, shash);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element hash = new Element().createElement(NAMESPACE, "Hash");
hash.addChild(Node.TEXT, shash);
header[0].addChild(Node.ELEMENT, hash);
Element ver = new Element().createElement(NAMESPACE, "Ver");
ver.addChild(Node.TEXT, "1.0");
header[0].addChild(Node.ELEMENT, ver);
// 创建SoapObject对象
SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
// SoapObject添加参数
if (properties != null) {
for (Iterator> it = properties.entrySet()
.iterator(); it.hasNext(); ) {
Map.Entry entry = it.next();
soapObject.addProperty(entry.getKey(), entry.getValue());
}
}
// 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号
final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 设置是否调用的是.Net开发的WebService
soapEnvelope.headerOut = header;
soapEnvelope.bodyOut = soapObject;
soapEnvelope.implicitTypes = true;// 去除子标签的i:type属性
soapEnvelope.dotNet = true;// 此处必须为true
httpTransportSE.debug = true;
soapEnvelope.setAddAdornments(false);// 去除body标签里的id、root属性
Log.d("webService:", "url:" + AppConfig.HuaWeiWebService);
Log.d("webService:", "method:" + methodName);
// 用于子线程与主线程通信的Handler
@SuppressLint("HandlerLeak")
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 将返回值回调到callBack的参数中
String retInfo = "";
if(TextUtils.isEmpty(AppConfig.Cookies)){
retInfo = "noCookie";
}
webServiceCallBack.callBack((SoapObject) msg.obj,retInfo);
}
};
// 开启线程去访问WebService
executorService.submit(new Runnable() {
@Override
public void run() {
SoapObject resultSoapObject = null;
List headerList = new ArrayList<>();
try {
HeaderProperty headerPropertyObj = new HeaderProperty("cookie", AppConfig.Cookies);
headerList.add(headerPropertyObj);
httpTransportSE.call(NAMESPACE + methodName, soapEnvelope,headerList);
if (soapEnvelope.getResponse() != null) {
// 获取服务器响应返回的SoapObject
resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
}
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//调试输出错误日志
if(resultSoapObject == null){
Log.d(TAG+"请求:",httpTransportSE.requestDump);
Log.d(TAG+"应答:",httpTransportSE.responseDump);
}
// 将获取的消息利用Handler发送到主线程
mHandler.sendMessage(mHandler.obtainMessage(0,
resultSoapObject));
}
}
});
}
/**
* @author xiaanming
*/
public interface CommonWebServiceCallRequest {
public void callBack(SoapObject soapObject,String retinfo);
}
}
POST /webservice/xxxx.PlatFormWS.asmx HTTP/1.1
User-Agent: ksoap2-android/2.6.0+
SOAPAction: http://xxxcom/xebService/xxxGetCheckNumber
Content-Type: text/xml;charset=utf-8
Connection: close
Accept-Encoding: gzip
Host: 192.168.51.28:8080
Content-Length: 676
HTTP/1.1 500 Internal Server Error
Server: nginx/1.17.0
Date: Thu, 13 Jun 2019 10:20:08 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 481
Connection: close
Cache-Control: private
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET