项目总结26:java调用webservice接口(asmx)

直接上源码

package com.hs.api.service.thirdparty.lab_asmx;

/* *
 *@Description:
 *@Author:TYJ
 *@Date: create in  2019/5/27 11:01
 */
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class ASMXWebservice {

    public static void main(String[] args) throws Exception {
        webserviceClient();
    }
    
    /**
     *@描述 
     *@参数  
     *@返回值  
     *@创建人  TangYujie
     *@创建时间  2019/5/27 21:34
     */
    public static void webserviceClient() throws Exception {
        HttpClient httpClient = new HttpClient();
        // httpClient.getHostConfiguration().setProxy("127.0.0.1", 8888);
        //接口地址。后面的TestMethod表示方法名
        String url = "http://XXX.XX.com:8000/Webjy.asmx/GetDataBYSysId";
        PostMethod postMethod = new PostMethod(url);
        //编码设为UTF-8
        postMethod.getParams().setParameter(
                HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        //请求参数
        NameValuePair[] data = {
                new NameValuePair("guid", "XXXX"),
                new NameValuePair("SysId", "XXXX"),
        };
        postMethod.setRequestBody(data);
        //调用webservice,返回statuscode。200表示成功。
        int statusCode = httpClient.executeMethod(postMethod);
        System.out.println("statusCode--->" + statusCode);
        //返回的Response文本。
        String responseData = postMethod.getResponseBodyAsString();
        System.out.println("responseData-->" + responseData);
    }
}

 

posted on 2019-05-27 21:37  我不吃番茄  阅读(522)  评论(0编辑  收藏  举报