HttpClient调用webservice
webservice调用总结
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; /** * 类作用:演示了如何通过get、post以及httpclient封装soap请求三种方式调用webservice * @author qsw-Myonlystar @date 2010-7-6 * @mail i@qinshuwei.com * 说明:调用的webservice为http://webservice.webxml.com.cn网站的火车站查询服务 */ public class Test { /** * 通过get方式演示webservice */ private void getMethodWS(){ DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationName"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = null; try { responseBody = httpclient.execute(httpget, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(responseBody); httpclient.getConnectionManager().shutdown(); } /** * 通过post方式调用webservice * @throws UnsupportedEncodingException */ private void postMethodWS(){ DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost( "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByTrainCode"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("TrainCode",null)); nvps.add(new BasicNameValuePair("UserID", null)); UrlEncodedFormEntity urlEntity = null; try { urlEntity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } httpost.setEntity(urlEntity); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = null; try { responseBody = httpclient.execute(httpost, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(responseBody); httpclient.getConnectionManager().shutdown(); } /** * 通过soap方式调用webservice * @throws IOException * @throws ClientProtocolException */ private void soapMethodWS(){ String soapRequestData = "<?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>" + "<getStationName xmlns=\"http://WebXml.com.cn/\" />" + "</soap:Body>" + "</soap:Envelope>"; DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx"); httpost.setHeader("Content-Type", "text/xml; charset=utf-8"); HttpEntity entity = null; try { entity = new StringEntity(soapRequestData); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } httpost.setEntity(entity); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = null; try { responseBody = httpclient.execute(httpost, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(responseBody); httpclient.getConnectionManager().shutdown(); } /** * @param args * @throws IOException * @throws ClientProtocolException */ public static void main(String[] args){ Test t=new Test(); t.getMethodWS();//get方式测试 t.soapMethodWS();//soap方式测试 t.postMethodWS();//post方式测试 } } |
