标签:android c class code java http
通过HttpClient请求webService
| 1 | <soap:Envelopexmlns:soap="http://www.w3.org/2003/05/soap-envelope"xmlns:sam="http://user.service.xxx.com"> | 
| 2 |    <soap:Header/> | 
| 3 |    <soap:Body> | 
| 4 |       <sam:getUserInfo> | 
| 5 |      <sam:userName>sunlightcs</sam:userName> | 
| 6 |       </sam:getUserInfo> | 
| 7 |    </soap:Body> | 
| 8 | </soap:Envelope> | 
| 01 | importjava.io.IOException; | 
| 02 | importjava.io.OutputStream; | 
| 03 | importjava.io.OutputStreamWriter; | 
| 04 | importjava.io.Writer; | 
| 05 | 
| 06 | importorg.apache.http.HttpResponse; | 
| 07 | importorg.apache.http.client.HttpClient; | 
| 08 | importorg.apache.http.client.methods.HttpPost; | 
| 09 | importorg.apache.http.entity.ContentProducer; | 
| 10 | importorg.apache.http.entity.EntityTemplate; | 
| 11 | importorg.apache.http.impl.client.DefaultHttpClient; | 
| 12 | importorg.apache.http.util.EntityUtils; | 
| 13 | 
| 14 | 
| 15 | publicclassClientTest { | 
| 16 | 
| 17 |     publicstaticvoidmain(String[] args) { | 
| 18 |         ClientTest.httpClientPost(); | 
| 19 |     } | 
| 20 |      | 
| 21 |     privatestaticvoidhttpClientPost() { | 
| 22 |         HttpClient client = newDefaultHttpClient(); | 
| 23 |         HttpPost post = newHttpPost("http://localhost:8080/xxx/services/userService"); | 
| 24 |          | 
| 25 |         try{ | 
| 26 |             ContentProducer cp = newContentProducer() { | 
| 27 |                 publicvoidwriteTo(OutputStream outstream) throwsIOException { | 
| 28 |                     Writer writer = newOutputStreamWriter(outstream,"UTF-8"); | 
| 29 |                      | 
| 30 |                     /** | 
| 31 |                      * 获取请求的xml格式数据 | 
| 32 |                      */ | 
| 33 |                     String requestXml = getRequestXml(); | 
| 34 |                     writer.write(requestXml); | 
| 35 |                     writer.flush(); | 
| 36 |                 } | 
| 37 |             }; | 
| 38 | 
| 39 |             post.setEntity(newEntityTemplate(cp)); | 
| 40 |             HttpResponse response = client.execute(post); | 
| 41 |              | 
| 42 |         //打印返回的xml数据 | 
| 43 |             System.out.println(EntityUtils.toString(response.getEntity())); | 
| 44 |         } catch(IOException e) { | 
| 45 |             e.printStackTrace(); | 
| 46 |         } | 
| 47 |     } | 
| 48 |      | 
| 49 |      | 
| 50 |     privatestaticString getRequestXml(){ | 
| 51 |         StringBuilder sb = newStringBuilder("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">"); | 
| 52 |         sb.append("<soap:Header/>"); | 
| 53 |         sb.append("<soap:Body>"); | 
| 54 |         sb.append("<sam:getUserInfo>"); | 
| 55 |         sb.append("<sam:userName>sunlightcs</sam:userName>"); | 
| 56 |         sb.append("</sam:getUserInfo>"); | 
| 57 |         sb.append("</soap:Body>"); | 
| 58 |         sb.append("</soap:Envelope>"); | 
| 59 |          | 
| 60 |         returnsb.toString(); | 
| 61 |     } | 
| 62 | 
| 63 | } | 
| 1 | <?xmlversion=‘1.0‘encoding=‘UTF-8‘?> | 
| 2 | <soapenv:Envelopexmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> | 
| 3 |     <soapenv:Body> | 
| 4 |         <ns:getUserInfoResponsexmlns:ns="http://user.service.xxx.com"> | 
| 5 |             <ns:return>xxx</ns:return> | 
| 6 |         </ns:getUserInfoResponse> | 
| 7 |     </soapenv:Body> | 
| 8 | </soapenv:Envelope> | 
通过HttpClient请求webService,布布扣,bubuko.com
标签:android c class code java http
原文地址:http://www.cnblogs.com/chenying99/p/3746797.html