您的位置:首页 > 理论基础 > 计算机网络

java httpclient作为客户端调用webservice

2016-12-13 17:22 633 查看
转载链接:httpclient作为客户端调用webservice

1.xml

SSOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
POST /ServiceApi.asmx HTTP/1.1
Host: 0.0.0.0
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.uradiosys.com/HostAddOrUpdateByOtherSys"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HostAddOrUpdateByOtherSys xmlns="http://www.uradiosys.com/">
<tagMac>string</tagMac>
<hostExternalId>string</hostExternalId>
<hostName>string</hostName>
<description>string</description>
<ImagePath>string</ImagePath>
</HostAddOrUpdateByOtherSys>
</soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HostAddOrUpdateByOtherSysResponse xmlns="http://www.uradiosys.com/">
<HostAddOrUpdateByOtherSysResult>
<Suceess>boolean</Suceess>
<ErrorCode>int</ErrorCode>
<ErrText>string</ErrText>
</HostAddOrUpdateByOtherSysResult>
</HostAddOrUpdateByOtherSysResponse>
</soap:Body>
</soap:Envelope>


SSOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.
POST /ServiceApi.asmx HTTP/1.1
Host: 0.0.0.0
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<HostAddOrUpdateByOtherSys xmlns="http://www.uradiosys.com/">
<tagMac>string</tagMac>
<hostExternalId>string</hostExternalId>
<hostName>string</hostName>
<description>string</description>
<ImagePath>string</ImagePath>
</HostAddOrUpdateByOtherSys>
</soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<HostAddOrUpdateByOtherSysResponse xmlns="http://www.uradiosys.com/">
<HostAddOrUpdateByOtherSysResult>
<Suceess>boolean</Suceess>
<ErrorCode>int</ErrorCode>
<ErrText>string</ErrText>
</HostAddOrUpdateByOtherSysResult>
</HostAddOrUpdateByOtherSysResponse>
</soap12:Body>
</soap12:Envelope>

2.java httpclient调用webservice代码
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.util.EntityUtils;

public class HttpClientCallSoapUtil {

    static int socketTimeout = 30000;// 请求超时时间

    static int connectTimeout = 30000;// 传输超时时间

    /**

     * 使用SOAP1.1发送消息

     *

     * @param postUrl

     * @param soapXml

     * @param soapAction

     * @return

     */

    public static String doPostSoap1_1(String postUrl, String soapXml,

                                       String soapAction) {

        String retStr = "";

        // 创建HttpClientBuilder

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // HttpClient

        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost(postUrl);

        //  设置请求和传输超时时间

        RequestConfig requestConfig = RequestConfig.custom()

                .setSocketTimeout(socketTimeout)

                .setConnectTimeout(connectTimeout).build();

        httpPost.setConfig(requestConfig);

        try {

            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

            httpPost.setHeader("SOAPAction", soapAction);

            StringEntity data = new StringEntity(soapXml,

                    Charset.forName("UTF-8"));

            httpPost.setEntity(data);

            CloseableHttpResponse response = closeableHttpClient

                    .execute(httpPost);

            HttpEntity httpEntity = response.getEntity();

            if (httpEntity != null) {

                // 打印响应内容

                retStr = EntityUtils.toString(httpEntity, "UTF-8");

                System.out.println("response:" + retStr);

            }

            // 释放资源

            closeableHttpClient.close();

        } catch (Exception e) {

            System.out.println("exception in doPostSoap1_1" + e);

        }

        return retStr;

    }

    /**

     * 使用SOAP1.2发送消息

     *

     * @param postUrl

     * @param soapXml

     * @param soapAction

     * @return

     */

    public static String doPostSoap1_2(String postUrl, String soapXml,

                                       String soapAction) {

        String retStr = "";

        // 创建HttpClientBuilder

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        // HttpClient

        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost(postUrl);

        // 设置请求和传输超时时间

        RequestConfig requestConfig = RequestConfig.custom()

                .setSocketTimeout(socketTimeout)

                .setConnectTimeout(connectTimeout).build();

        httpPost.setConfig(requestConfig);

        try {

            httpPost.setHeader("Content-Type",

                    "application/soap+xml;charset=UTF-8");

            httpPost.setHeader("SOAPAction", soapAction);

            StringEntity data = new StringEntity(soapXml,

                    Charset.forName("UTF-8"));

            httpPost.setEntity(data);

            CloseableHttpResponse response = closeableHttpClient

                    .execute(httpPost);

            HttpEntity httpEntity = response.getEntity();

            if (httpEntity != null) {

                // 打印响应内容

                retStr = EntityUtil
ac1a
s.toString(httpEntity, "UTF-8");

                System.out.println("response:" + retStr);

            }

            // 释放资源

            closeableHttpClient.close();

        } catch (Exception e) {

            System.out.println("exception in doPostSoap1_2"+ e);

        }

        return retStr;

    }

    public static void main(String[] args) {

        String tagMac = "B0:8E:1A:50:54:AC";

        String hostExternalId = "5054AC"; //编号  自定义的

        String hostName = "李艳军";  //用户姓名

        String description = "";  //用户描述,可以是一些附加信息

        String ImagePath = "";

        //webservice地址

        String postUrl = "http://0.0.0.0:8050/ServiceApi.asmx?wsdl";

        //SOAPAction

        String SOAPAction = "http://www.uradiosys.com/HostAddOrUpdateByOtherSys";

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +

                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +

                "  <soap:Body>\n" +

                "    <HostAddOrUpdateByOtherSys xmlns=\"http://www.uradiosys.com/\">\n" +

                "      <tagMac>"+ tagMac +"</tagMac>\n" +

                "      <hostExternalId>"+ hostExternalId +"</hostExternalId>\n" +

                "      <hostName>"+ hostName +"</hostName>\n" +

                "      <description></description>\n" +

                "      <ImagePath></ImagePath>\n" +

                "    </HostAddOrUpdateByOtherSys>\n" +

                "  </soap:Body>\n" +

                "</soap:Envelope>";

        //采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务

//        doPostSoap1_1(postUrl, xml, SOAPAction);

        String soap12 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +

                "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +

                "  <soap12:Body>\n" +

                "    <HostAddOrUpdateByOtherSys xmlns=\"http://www.uradiosys.com/\">\n" +

                "      <tagMac>"+ tagMac +"</tagMac>\n" +

                "      <hostExternalId>"+ hostExternalId +"</hostExternalId>\n" +

                "      <hostName>"+ hostName +"</hostName>\n" +

                "      <description></description>\n" +

                "      <ImagePath></ImagePath>\n" +

                "    </HostAddOrUpdateByOtherSys>\n" +

                "  </soap12:Body>\n" +

                "</soap12:Envelope>";

        //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务

        doPostSoap1_2(postUrl, soap12, "");

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: