java调用asmx的webservice

使用apache的httpclient组件进行调用。

需要的包:commons-codec-1.5.jar, commons-httpclient-3.1.jar, commons-logging-1.1.jar

调用代码如下:

 1     private static void clientDemo() throws HttpException, IOException {
 2         HttpClient httpClient = new HttpClient();
 3         // httpClient.getHostConfiguration().setProxy("127.0.0.1", 8888);
 4         //接口地址。后面的TestMethod表示方法名
 5         String url = "http://xxx.xxx.xxx.xxx:8089/service1.asmx/TestMethod";    
 6         PostMethod postMethod = new PostMethod(url);
 7         
 8         //编码设为UTF-8
 9         postMethod.getParams().setParameter(
10                 HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");    
11         
12         //NameValuePair 名值对。传入webservice的参数
13         NameValuePair[] data = {
14                 new NameValuePair("name", "admin"),
15                 new NameValuePair("password", "admin"),
16                 new NameValuePair("message", "测试测试"),
17         };
18         postMethod.setRequestBody(data);
19         //调用webservice,返回statuscode。200表示成功。
20         int statusCode = httpClient.executeMethod(postMethod);    
21         System.out.println(statusCode);
22         //返回的Response文本。
23         String soapRequestData = postMethod.getResponseBodyAsString();    
24         System.out.println(soapRequestData);
25     }

 

posted @ 2014-07-07 11:43  yuhuhu  阅读(1997)  评论(0编辑  收藏  举报