参考:http://aperise.iteye.com/blog/2223454
http://blog.csdn.net/chenleixing/article/details/43456987
1 package explore; 2 3 import java.io.IOException; 4 import java.nio.charset.Charset; 5 6 import org.apache.commons.lang.StringEscapeUtils; 7 import org.apache.http.HttpEntity; 8 import org.apache.http.ParseException; 9 import org.apache.http.client.ClientProtocolException; 10 import org.apache.http.client.config.RequestConfig; 11 import org.apache.http.client.methods.CloseableHttpResponse; 12 import org.apache.http.client.methods.HttpPost; 13 import org.apache.http.entity.StringEntity; 14 import org.apache.http.impl.client.CloseableHttpClient; 15 import org.apache.http.impl.client.HttpClientBuilder; 16 import org.apache.http.util.EntityUtils; 17 18 import util.IOUtil; 19 20 public class HttpClientWay1 { 21 22 static int socketTimeout = 30000;// 请求超时时间 23 static int connectTimeout = 30000;// 传输超时时间 24 25 public static void main(String[] args) { 26 try { 27 // wsdl 28 // http://fw1test.shdzfp.com:7500/axis2/services/SajtIssueInvoiceService 29 String con; 30 31 // 组织推送报文 32 // con = GenerateXml.shift_plus("pt_kpttxx.xml"); 33 con = IOUtil.msgMachine("my.xml"); 34 System.out.println(con); 35 36 // 创建HttpClientBuilder 37 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 38 // HttpClient 39 CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); 40 HttpPost httpPost = new HttpPost("http://fw1test.shdzfp.com:7500/axis2/services/SajtIssueInvoiceService"); 41 42 // 设置请求和传输超时时间 43 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout) 44 .setConnectTimeout(connectTimeout).build(); 45 httpPost.setConfig(requestConfig); 46 47 httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); 48 httpPost.setHeader("SOAPAction", ""); 49 StringEntity data = new StringEntity(con, Charset.forName("UTF-8")); 50 httpPost.setEntity(data); 51 CloseableHttpResponse response = closeableHttpClient.execute(httpPost); 52 HttpEntity httpEntity = response.getEntity(); 53 if (httpEntity != null) { 54 // 打印响应内容 55 String retStr = EntityUtils.toString(httpEntity, "UTF-8"); 56 retStr = StringEscapeUtils.unescapeXml(retStr);// 反转义 57 System.out.println(retStr); 58 } 59 // 释放资源 60 closeableHttpClient.close(); 61 } catch (ClientProtocolException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } catch (ParseException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } catch (IOException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } catch (Exception e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } 74 } 75 76 }
附 wsdl 内容:
1 This XML file does not appear to have any style information associated with it. The document tree is shown below. 2 <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://service.sl.shdzfp.sajt" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://service.sl.shdzfp.sajt"> 3 <wsdl:types> 4 <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.sl.shdzfp.sajt"> 5 <xs:element name="eiInterface"> 6 <xs:complexType> 7 <xs:sequence> 8 <xs:element minOccurs="0" name="in0" nillable="true" type="xs:string"/> 9 </xs:sequence> 10 </xs:complexType> 11 </xs:element> 12 <xs:element name="eiInterfaceResponse"> 13 <xs:complexType> 14 <xs:sequence> 15 <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/> 16 </xs:sequence> 17 </xs:complexType> 18 </xs:element> 19 </xs:schema> 20 </wsdl:types> 21 <wsdl:message name="eiInterfaceRequest"> 22 <wsdl:part name="parameters" element="ns:eiInterface"></wsdl:part> 23 </wsdl:message> 24 <wsdl:message name="eiInterfaceResponse"> 25 <wsdl:part name="parameters" element="ns:eiInterfaceResponse"></wsdl:part> 26 </wsdl:message> 27 <wsdl:portType name="SajtIssueInvoiceServicePortType"> 28 <wsdl:operation name="eiInterface"> 29 <wsdl:input message="ns:eiInterfaceRequest" wsaw:Action="urn:eiInterface"></wsdl:input> 30 <wsdl:output message="ns:eiInterfaceResponse" wsaw:Action="urn:eiInterfaceResponse"></wsdl:output> 31 </wsdl:operation> 32 </wsdl:portType> 33 <wsdl:binding name="SajtIssueInvoiceServiceSoap11Binding" type="ns:SajtIssueInvoiceServicePortType"> 34 <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 35 <wsdl:operation name="eiInterface"> 36 <soap:operation soapAction="urn:eiInterface" style="document"/> 37 <wsdl:input> 38 <soap:body use="literal"/> 39 </wsdl:input> 40 <wsdl:output> 41 <soap:body use="literal"/> 42 </wsdl:output> 43 </wsdl:operation> 44 </wsdl:binding> 45 <wsdl:binding name="SajtIssueInvoiceServiceSoap12Binding" type="ns:SajtIssueInvoiceServicePortType"> 46 <soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 47 <wsdl:operation name="eiInterface"> 48 <soap12:operation soapAction="urn:eiInterface" style="document"/> 49 <wsdl:input> 50 <soap12:body use="literal"/> 51 </wsdl:input> 52 <wsdl:output> 53 <soap12:body use="literal"/> 54 </wsdl:output> 55 </wsdl:operation> 56 </wsdl:binding> 57 <wsdl:binding name="SajtIssueInvoiceServiceHttpBinding" type="ns:SajtIssueInvoiceServicePortType"> 58 <http:binding verb="POST"/> 59 <wsdl:operation name="eiInterface"> 60 <http:operation location="SajtIssueInvoiceService/eiInterface"/> 61 <wsdl:input> 62 <mime:content part="eiInterface" type="text/xml"/> 63 </wsdl:input> 64 <wsdl:output> 65 <mime:content part="eiInterface" type="text/xml"/> 66 </wsdl:output> 67 </wsdl:operation> 68 </wsdl:binding> 69 <wsdl:service name="SajtIssueInvoiceService"> 70 <wsdl:port name="SajtIssueInvoiceServiceHttpSoap11Endpoint" binding="ns:SajtIssueInvoiceServiceSoap11Binding"> 71 <soap:address location="http://fw1test.shdzfp.com:7500/axis2/services/SajtIssueInvoiceService.SajtIssueInvoiceServiceHttpSoap11Endpoint/"/> 72 </wsdl:port> 73 <wsdl:port name="SajtIssueInvoiceServiceHttpEndpoint" binding="ns:SajtIssueInvoiceServiceHttpBinding"> 74 <http:address location="http://fw1test.shdzfp.com:7500/axis2/services/SajtIssueInvoiceService.SajtIssueInvoiceServiceHttpEndpoint/"/> 75 </wsdl:port> 76 </wsdl:service> 77 </wsdl:definitions>