WebService接口的调用demo代码

因为项目中有需要调用第三方系统接口,而第三方系统是一个很老的系统,只支持webService接口的调用,因而不得不再去熟悉这种老接口

下面是代码,亲测有效:

需要引入的包名
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
String soapRequestData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:in=\"http://in.webservice.organization.sys.kmss.landray.com/\">\n" +
        "   <soapenv:Header/>\n" +
        "   <soapenv:Body>\n" +
        "      <in:syncOrgElements>\n" +
        "         <arg0>\n" +
        "            <appName></appName>\n" +
        "            <orgJsonData>[\n" +
        "  {\n" +
        "    \"id\": \"12d552352ac1dd47c57c307401f958f7\",\n" +
        "    \"lunid\": \"12d552352ac1dd47c57c307401f958f7\", \n" +
        "    \"name\": \"测试\", \n" +
        "    \"type\": \"dept\"\n" +
        "\t}\n" +
        "]</orgJsonData>\n" +
        "            <orgSyncConfig></orgSyncConfig>\n" +
        "         </arg0>\n" +
        "      </in:syncOrgElements>\n" +
        "   </soapenv:Body>\n" +
        "</soapenv:Envelope>";

 

如上是webService接口的请求报文。

报文可以直接把该接口地址贴到soapUI,即可看到Request的报文。

//将接口地址放到PostMethod中
PostMethod postMethod = new PostMethod("http://192.168.17.41:8080/sys/webservice/sysSynchroSetOrgWebService?wsdl");
try {
    soapRequestData.trim();
    System.out.println(soapRequestData);
    byte[] b = soapRequestData.getBytes("utf-8");
    InputStream is = new ByteArrayInputStream(b, 0, b.length);
    RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml");
    postMethod.setRequestEntity(re);
    postMethod.setRequestHeader("SOAPAction",
            "");
    postMethod.getParams().setParameter(
            HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
    HttpClient httpClient = new HttpClient();
    int status = httpClient.executeMethod(postMethod);
    byte[] responseBody = postMethod.getResponseBody();
    postMethod.releaseConnection();
    String result = new String(responseBody, "UTF-8");
    if (result != null){
        result = result.trim();
    }需要引入的jar包:<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>4.5.8</version>
</dependency>
<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>
</dependency>

debug调试,发现status结果为200,表示调用成功

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页