WebService接口调试如此简单

简介: WebService接口调试如此简单

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


<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header><n0:CredentialSoapHeader xmlns:n0="http://NewCap.com/NewCapecWebService/">
<n0:ID>1</n0:ID><n0:AppTypeID>6</n0:AppTypeID>
<n0:IP>11.11.11.12</n0:IP>
<n0:Timestamp>2019-06-14T08:57:39</n0:Timestamp>
<n0:Random>1</n0:Random>
<n0:Hash>KwEul5zOhWy8lnXdVeDiaw==</n0:Hash>
<n0:Ver>1.0</n0:Ver>
</n0:CredentialSoapHeader>
</v:Header>
<v:Body><GetCheckNumber xmlns="http://NewCap.com/NewCapecWebService/" id="o0" c:root="1">
<cardno i:type="d:string">1454236707</cardno></GetCheckNumber>
</v:Body>
</v:Envelope>


至此,全部结束,直接点击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<String, Object> 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<Map.Entry<String, Object>> it = properties.entrySet()
                    .iterator(); it.hasNext(); ) {
                Map.Entry<String, Object> 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<HeaderProperty> 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
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header><n0:CredentialSoapHeader xmlns:n0="http://xxx.com/xxxxWebService/"><n0:ID>0</n0:ID><n0:AppTypeID>0</n0:AppTypeID><n0:IP>1xxxx.0</n0:IP><n0:Timestamp>20190613182015</n0:Timestamp><n0:Random>1</n0:Random><n0:Hash>oevxmJAN+EvP1Wh0zPDSSg==
</n0:Hash><n0:Ver>1.0</n0:Ver></n0:CredentialSoapHeader></v:Header><v:Body><GetCheckNumber xmlns="http://NewCap.com/NewCapecWebService/"><cardno>1454236707</cardno></GetCheckNumber></v:Body></v:Envelope>
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
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>.............................. ---> XML ......(1, 415)............... ---> ............20190613182015.................. AllXsd ......</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
相关文章
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
205 0
|
5天前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
23 5
|
2月前
|
XML 前端开发 Java
JAVA调试webservice接口
JAVA调试webservice接口
39 0
|
5月前
|
XML Java API
使用WebService接口进行数据通信
使用WebService接口进行数据通信
|
5月前
|
XML Java API
使用WebService接口进行数据通信
使用WebService接口进行数据通信
|
7月前
|
存储 监控 BI
HIS系统是什么?一套前后端分离云HIS系统源码 接口技术RESTful API + WebSocket + WebService
医院管理信息系统(全称为Hospital Information System)即HIS系统。 常规模版包括门诊管理、住院管理、药房管理、药库管理、院长查询、电子处方、物资管理、媒体管理等,为医院管理提供更有力的保障。 HIS系统以财务信息、病人信息和物资信息为主线,通过对信息的收集、存储、传递、统计、分析、综合查询、报表输出和信息共享,及时为医院领导及各部门管理人员提供全面、准确的各种数据。 门诊医生工作站采用下拉式汉化菜单,界面友好,实用性强,设有与门诊挂号收费系统、医技科室信息系统、住院结算信息系统的软件接口。
171 1
使用队列解决高并发下使用Client对象调用webService接口
使用队列解决高并发下使用Client对象调用webService接口
|
网络安全 网络架构
https请求SOAP webService接口
https请求SOAP webService接口
155 0
|
XML JSON 网络协议
webService接口对接医院lis系统接口
webService接口对接医院lis系统接口
407 0
|
XML 测试技术 数据安全/隐私保护
【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)
【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)
319 0