首先,看看webservice服务调用演示:

a) 登录http://www.webxml.com.cn

b) 单击手机查询服务

c) 选择要调用的方法 例如: getMobileCodeInfo

输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null

返回结果:

HttpClient工具调用WS服务接口

HttpClient使用步骤如下:

  1. 创建 HttpClient 的实例
  2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
  3. 配置要传输的参数,和消息头信息
  4. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
  5. 通过response读取字符串

6. 释放连接。无论执行方法是否成功,都必须释放连接

代码:下面是分别发送http get、post和soap post请求:


import java.io.FileInputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; public class MyHttpClient
{
//webservice请求地址
private static String wsURL=
"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//创建HttpClient 的实例
private static HttpClient httpClient = new HttpClient();
/**
* 发送get请求
*/
public static void sendGet() throws Exception{
//创建get对象
GetMethod get = new GetMethod(wsURL + "?mobileCode=18323455678&userID=");
//发送get请求
int resultCode = httpClient.executeMethod(get);
System.out.println("Get请求结果:resultCode="+resultCode +", message="+get.getResponseBodyAsString());
//释放get请求资源
get.releaseConnection();
} /**
* 发送post请求
*/
public static void sendPost() throws Exception{
//创建get对象
PostMethod post = new PostMethod(wsURL);
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
post.setRequestBody("mobileCode=18323455678&userID=");
//发送get请求
int resultCode = httpClient.executeMethod(post);
System.out.println("Post请求结果:resultCode="+resultCode +", message="+post.getResponseBodyAsString());
//释放get请求资源
post.releaseConnection();
} /**
* soap方式的post请求
* soap:在http的基础上,可以发送xml格式数据
*/
public static void sendSoapPost() throws Exception {
// 创建一个post请求,类似Post请求
PostMethod postMethod = new PostMethod("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
// 设置传送信息的格式
postMethod.setRequestHeader("Content-Type","text/xml; charset=utf-8");
postMethod.setRequestBody(new FileInputStream("C:/a.txt"));
int code = httpClient.executeMethod(postMethod);
System.out.println("SOAP请求start..." + code);
System.out.println("消息码为:" + code);
System.out.println("返回的消息为:" + postMethod.getResponseBodyAsString());
postMethod.releaseConnection();
} public static void main(String[] args) throws Exception
{
sendGet();
sendPost();
sendSoapPost();
}
}

 

查看运行结果:

Get请求结果:resultCode=200, message=<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string> Post请求结果:resultCode=200, message=<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string> SOAP请求start...
消息码为:200
返回的消息为:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"><getMobileCodeInfoResult>18323455678:重庆 重庆 重庆移动全球通卡</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>

So easy Webservice 3.使用HttpClient工具调用Webservice接口的更多相关文章

  1. Java调用WebService方法总结(4)--Axis调用WebService

    Axis是比较常用的WebService框架,该项目在2006实现了最终版,后面就没有更新了.文中demo所使用到的软件版本:Java 1.8.0_191.Axis 1.4. 1.准备 参考Java调 ...

  2. 浅谈WebService开发三(动态调用WebService)转

    在前两讲里,我已经向大家演示了如何使用WebService.同步, 异步调用WebService,而在实际开发过程中,可能会有多个WebService接口供你选择,而在程序执行过程中才决定使用哪一个 ...

  3. Httpclient远程调用WebService示例(Eclipse+httpclient)

    package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...

  4. cxf 和 httpclient 客户端调用 webservice 接口

    一.cxf 生成 webservice 客户端 1.接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 2.进入你需要放置 webservi ...

  5. Java调用WebService方法总结(3)--wsimport调用WebService

    wsimport是JDK自带的把WSDL转成Java的工具,可以很方便的生成调用WebService的代码.文中所使用到的软件版本:Java 1.8.0_191. 1.准备 参考Java调用WebSe ...

  6. JAVA6开发WebService (四)——SAAJ调用WebService

    转载自http://wuhongyu.iteye.com/blog/810571 前面写了个JAX-WS的小例子,看到用JAVA6开发WebService确实很简单,也很方便,不过前面也说了,JAVA ...

  7. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  8. C#开发WEBService服务 C++开发客户端调用WEBService服务

    编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...

  9. Java调用WebService方法总结(7)--CXF调用WebService

    CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...

随机推荐

  1. 关闭 Windows 的常用端口

    netstat -ano 可以看到目前开着哪些端口 netstat -ano|findstr <端口号>   可以找到开放的端口的那条,最后还列出了 PID. 然后到任务管理器中,你可以查 ...

  2. Java中main方面面试题

    1.不用main方法如何定义一个类? 不行,没有main方法我们不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7开始就行不通了. 2.main() ...

  3. Android NDK 开发(二) -- 从Hlello World学起【转】

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/41805719  上篇文章讲述了Android NDK开发的一些基本概念,以及NDK ...

  4. 【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素

    问题:找出一个元素序列中出现次数最多的元素是什么 解决方案:collections模块中的Counter类正是为此类问题所设计的.它的一个非常方便的most_common()方法直接告诉你答案. # ...

  5. 161028、Nginx负载均衡实现tomcat集群方案简要小结

    重点两部分:一.负载均衡二.tomcat集群 所谓tomcat集群,就是可以向外提供并行服务的多台机器,任何一台服务器宕机,其它服务器可以替代它向外提供服务,而不影响用户访问. Nginx是一个常用的 ...

  6. jquery选择器中两个class是什么意思?

    jquery选择器中两个class是什么意思? $(".class1 .class2") 选择class1元素下class2的元素(中间有空格)$(".class1.cl ...

  7. wghd的git代码仓库分支管理说明【转】

    英文原文:http://www.nvie.com/posts/a-successful-git-branching-model/ 原文作者:Vincent Driessen 本文经Linux大棚博主总 ...

  8. Android的init过程(二):初始化语言(init.rc)解析【转】

    转自:http://www.cnblogs.com/nokiaguy/p/3164799.html Android的init过程(一) 本文使用的软件版本 Android:4.2.2 Linux内核: ...

  9. minio-dotnet --云存储服务

    inio是一家成立于2014年的生产开源云存储产品的新兴创业公司.这家创业公司是其创始人继Gluester之后的又一杰作,Gluester公司已经在2011年被Red Hat公司以1.36亿美元的价格 ...

  10. Hosting custom WPF calendar control in AX 2012

    原作者: https://community.dynamics.com/ax/b/axilicious/archive/2013/05/20/hosting-custom-wpf-calendar-c ...