首先,看看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. nginx 优化

    隐藏nginx版本号:在http标签内写server_tokens off; 隐藏apache版本号:ServerTokens Prod ServerSignature Off 更改nginx默认的用 ...

  2. ubuntu安装最新版本的node.js

    下面的方法适用于最新版本的Ubuntu.Ubuntu 12.04 LTS.Ubuntu 12.10.Ubuntu 13.04等版本.它可以帮助开发者在Ubuntu上安装Node.js,无需从头编译安装 ...

  3. javaWeb 使用jsp标签进行防盗链

    /** * 1.新建类继承SimpleTagSupport * 新建2个属性, 添加对应的set方法 * 覆盖doTag()方法 */ import java.io.IOException; impo ...

  4. iOS抓包Charles 操作

    今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包 http://blog.csdn.net/jiangwei0910410003/article/details/198 ...

  5. wordpress网站被挂马以及防御方法

    wordpress本身的安全性是非常的高的,一般不会被轻易的破解,被挂马,但是我们也不能够过度迷信wordpress的安全性,凡是连接上互联网的服务器和电脑,都存在被破解的风险性.所以我们在日常维护自 ...

  6. IE6兼容透明背景图

    JS代码: <!--[if IE 6]><script src="~/Scripts/UI/DD_belatedPNG.js"></script> ...

  7. 5、XML(1)

    1 XML入门 1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互. 负责静态的网页制作的语言 HTML语言特点: 1)由 ...

  8. postgresql之distinct用法

    1. 去重:关键字distinct去重功能  在其他数据库(oracle,mysql)是存在:当然postgresql也有这个功能 [postgres@sdserver40_210 ~]$ psql ...

  9. java UUID

    UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符.UUID具有以下涵义: 经由一定的算法机器生成 为了保证 ...

  10. 通过HtmlEmail 发送邮件

    今天第一次来上海市虹口图书馆上自习,感觉还是很爽的.自己撸代码学会了发送邮件.啥都不说了,直接撸代码吧! 首先 必须引进来三个jar包: compile 'javax.mail:mail:1.4.7' ...