Httpclient遠端呼叫WebService示例(Eclipse+httpclient)
阿新 • • 發佈:2019-02-16
1 package com.http; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.InputStream; 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.util.Set; 8 9 import org.apache.commons.httpclient.HttpClient; 10 import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 11 import org.apache.commons.httpclient.methods.PostMethod; 12 import org.apache.commons.httpclient.methods.RequestEntity; 13 14 // 動態構造呼叫串,靈活性更大 15 public class DynamicHttpclientCall { 16 17 private String namespace; 18 private String methodName; 19 private String wsdlLocation; 20 private String soapResponseData; 21 22 public DynamicHttpclientCall(String namespace, String methodName, 23 String wsdlLocation) { 24 25 this.namespace = namespace; 26 this.methodName = methodName; 27 this.wsdlLocation = wsdlLocation; 28 } 29 30 private int invoke(Map<String, String> patameterMap) throws Exception { 31 PostMethod postMethod = new PostMethod(wsdlLocation); 32 String soapRequestData = buildRequestData(patameterMap); 33 34 byte[] bytes = soapRequestData.getBytes("utf-8"); 35 InputStream inputStream = new ByteArrayInputStream(bytes, 0, 36 bytes.length); 37 RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, 38 bytes.length, "application/soap+xml; charset=utf-8"); 39 postMethod.setRequestEntity(requestEntity); 40 41 HttpClient httpClient = new HttpClient(); 42 int statusCode = httpClient.executeMethod(postMethod); 43 soapResponseData = postMethod.getResponseBodyAsString(); 44 45 return statusCode; 46 } 47 48 private String buildRequestData(Map<String, String> patameterMap) { 49 StringBuffer soapRequestData = new StringBuffer(); 50 soapRequestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 51 soapRequestData 52 .append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" 53 + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" 54 + " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"); 55 soapRequestData.append("<soap12:Body>"); 56 soapRequestData.append("<" + methodName + " xmlns=\"" + namespace 57 + "\">"); 58 soapRequestData.append("<" + methodName + "Request>"); 59 60 Set<String> nameSet = patameterMap.keySet(); 61 for (String name : nameSet) { 62 soapRequestData.append("<" + name + ">" + patameterMap.get(name) 63 + "</" + name + ">"); 64 } 65 66 soapRequestData.append("</" + methodName + "Request>"); 67 soapRequestData.append("</" + methodName + ">"); 68 soapRequestData.append("</soap12:Body>"); 69 soapRequestData.append("</soap12:Envelope>"); 70 71 return soapRequestData.toString(); 72 } 73 74 /** 75 * @param args 76 * @throws Exception 77 */ 78 public static void main(String[] args) throws Exception { 79 // TODO Auto-generated method stub 80 81 DynamicHttpclientCall dynamicHttpclientCall = new DynamicHttpclientCall( 82 "http://shippingapi.ebay.cn/", "GetAPACShippingPackage", 83 "http://epacketws.pushauction.net/v3/orderservice.asmx?wsdl"); 84 85 Map<String, String> patameterMap = new HashMap<String, String>(); 86 87 patameterMap.put("TrackCode", "123"); 88 patameterMap.put("Version", "123"); 89 patameterMap.put("APIDevUserID", "123"); 90 patameterMap.put("APIPassword", "123"); 91 patameterMap.put("APISellerUserID", "123"); 92 patameterMap.put("MessageID", "123"); 93 patameterMap.put("TrackCode", "123"); 94 95 String soapRequestData = dynamicHttpclientCall.buildRequestData(patameterMap); 96 System.out.println(soapRequestData); 97 98 int statusCode = dynamicHttpclientCall.invoke(patameterMap); 99 if(statusCode == 200) { 100 System.out.println("呼叫成功!"); 101 System.out.println(dynamicHttpclientCall.soapResponseData); 102 } 103 else { 104 System.out.println("呼叫失敗!錯誤碼:" + statusCode); 105 } 106 107 } 108 109 }