1. 程式人生 > >Post傳送SOAP格式內容訪問WebService介面

Post傳送SOAP格式內容訪問WebService介面

需要jar包:

commons-logging-1.0.4.jar
httpclient-4.3.2.jar
httpcore-4.3.2.jar

測試:

package http;

public class TestSoap {
    public static void main(String[] args) {
        String soapXml="<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                "<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>" + " <execute xmlns=\"webservices.wst.weaver.com.cn\">" + " <in0>wer</in0>" + " <in1
>
2</in1>" + " </execute>" + " </soap:Body>" + "</soap:Envelope>"; String result=HttpToSoap.doPostSoap("http://127.0.0.1:8087//services/WebServicesTest", soapXml, "", HttpToSoap.soap); System.out.println(result); } }

程式碼:

package http;

import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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 HttpToSoap {
    static int socketTimeout = 10000;// 請求超時時間
    static int connectTimeout = 10000;// 傳輸超時時間
    static final String soap = "text/xml;charset=UTF-8";
    static final String soap11 = "application/soap+xml;charset=UTF-8";

    /**
     * 同步HttpPost請求傳送SOAP格式的訊息
     * 
     * @param webServiceURL
     *            WebService介面地址
     * @param soapXml
     *            訊息體
     * @param soapAction
     *            soapAction
     * @param soapType
     *            soap版本
     * @return
     */
    public static String doPostSoap(String webServiceURL, String soapXml,
            String soapAction, String soapType) {
        // 建立HttpClient
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        // 建立Post請求
        HttpPost httpPost = new HttpPost(webServiceURL);
        // 設定請求和傳輸超時時間
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        // 設定Post請求報文頭部
        httpPost.setHeader("Content-Type", soapType);
        httpPost.setHeader("SOAPAction", soapAction);
        // 新增報文內容
        StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
        httpPost.setEntity(data);
        try {
            // 執行請求獲取返回報文
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 列印響應內容
                return EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 釋放資源
            closeableHttpClient.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}