前面有一篇使用HttpClient调用带参数的post接口方法,这里找到一篇使用HttpClient调用Soap协议接口的方式。
原文地址:httpclient妙用一 httpclient作为客户端调用webservice
备注:HttpClient可以同时传递带参数的头和body,都是在setEntity方法中
import java.io.IOException; import java.net.SocketTimeoutException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class CallWebService { public static void main(String[] args) { CallWebService oAUtil = new CallWebService(); String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.gacl.me/\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <ws:sayHello>\n" + " <arg0>Hello World!</arg0>\n" + " </ws:sayHello>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; sendHttpSoapAudit(soapXml, ""); } public static void sendHttpSoapAudit(String soapXml,String soapAction) { System.out.println(System.currentTimeMillis()); String oaRestfulServiceUrl = null; // am.getOADBTransaction().getProfile(""); oaRestfulServiceUrl = "http://192.168.65.84:8989/WS_Server/Webservice?wsdl"; // RequestConfig requestConfig = // RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000) .build(); CloseableHttpClient httpclient = null; CloseableHttpResponse h_response = null; try { // httpclient = // HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); httpclient = HttpClientBuilder.create().build(); HttpPost postMethod = new HttpPost(oaRestfulServiceUrl); // postMethod.setConfig(requestConfig); postMethod.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"); postMethod.setHeader("Referer", oaRestfulServiceUrl); List<NameValuePair> params = new ArrayList<NameValuePair>(); // 添加参数 params.add(new BasicNameValuePair("type", type)); params.add(new BasicNameValuePair("param", param)); postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); postMethod.setHeader("Content-Type", "text/xml;charset=UTF-8"); postMethod.setHeader("SOAPAction", soapAction); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); postMethod.setEntity(data); h_response = httpclient.execute(postMethod); HttpEntity repEntity = h_response.getEntity(); int statusCode = h_response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { postMethod.abort(); System.out.println("调用异常 " + statusCode); } String content = EntityUtils.toString(repEntity, "UTF-8"); System.out.println("content "+content); } catch (ConnectTimeoutException e) { System.out.println(" ConnectTimeoutException " + System.currentTimeMillis()); e.printStackTrace(); } catch (SocketTimeoutException e) { System.out.println(" SocketTimeoutException " + System.currentTimeMillis()); e.printStackTrace(); } catch (Exception e) { System.out.println("Exception " + System.currentTimeMillis()); e.printStackTrace(); } finally { if (h_response != null) { try { h_response.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } } }