• 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     }
  • 相关阅读:
    linux下使用g++编译cpp工程
    c++字符串互相转换
    MFC双缓冲绘图实例
    Python进阶之迭代器和生成器
    <大话设计模式>笔记
    配置程序成为Linux服务
    Django的设计模式
    Django ModelForm修改默认的控件属性
    Android活动生命周期
    MySQL必知必会笔记
  • 原文地址:https://www.cnblogs.com/skyeyh/p/3829358.html
Copyright © 2020-2023  润新知