1. 程式人生 > >java httpclient作為客戶端呼叫webservice

java httpclient作為客戶端呼叫webservice

轉載連結: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 = EntityUtils.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, "");
    }
}