So easy Webservice 3.使用HttpClient工具调用Webservice接口
首先,看看webservice服务调用演示:
b) 单击手机查询服务
c) 选择要调用的方法 例如: getMobileCodeInfo
输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null

返回结果:

HttpClient工具调用WS服务接口:
HttpClient使用步骤如下:
- 创建 HttpClient 的实例
- 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址
- 配置要传输的参数,和消息头信息
- 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
- 通过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接口的更多相关文章
- Java调用WebService方法总结(4)--Axis调用WebService
Axis是比较常用的WebService框架,该项目在2006实现了最终版,后面就没有更新了.文中demo所使用到的软件版本:Java 1.8.0_191.Axis 1.4. 1.准备 参考Java调 ...
- 浅谈WebService开发三(动态调用WebService)转
在前两讲里,我已经向大家演示了如何使用WebService.同步, 异步调用WebService,而在实际开发过程中,可能会有多个WebService接口供你选择,而在程序执行过程中才决定使用哪一个 ...
- Httpclient远程调用WebService示例(Eclipse+httpclient)
package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...
- cxf 和 httpclient 客户端调用 webservice 接口
一.cxf 生成 webservice 客户端 1.接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx 2.进入你需要放置 webservi ...
- Java调用WebService方法总结(3)--wsimport调用WebService
wsimport是JDK自带的把WSDL转成Java的工具,可以很方便的生成调用WebService的代码.文中所使用到的软件版本:Java 1.8.0_191. 1.准备 参考Java调用WebSe ...
- JAVA6开发WebService (四)——SAAJ调用WebService
转载自http://wuhongyu.iteye.com/blog/810571 前面写了个JAX-WS的小例子,看到用JAVA6开发WebService确实很简单,也很方便,不过前面也说了,JAVA ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- C#开发WEBService服务 C++开发客户端调用WEBService服务
编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...
- Java调用WebService方法总结(7)--CXF调用WebService
CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...
随机推荐
- 介绍“Razor”— ASP.NET的一个新视图引擎
我的团队当前正在从事的工作之一就是为ASP.NET添加一个新的视图引擎. 一直以来,ASP.NET MVC都支持 “视图引擎”的概念—采用不同语法的模板的可插拔模块.当前ASP.NET MVC “默认 ...
- Mac OX 隐藏文件夹,文件,应用,磁盘的2种方法 hide finder folder, file, application, volume in 2 ways
经常需要主目录下隐藏一些文件夹之类的, 第一想到的当然就是:在要隐藏的文件夹前面加『.』(leading dot),这个用法当然可以的了 用习惯了Linux/GNU系统的,基本习惯使用这种办法 但是, ...
- oracle 11g升级
手先需要下载oracle11g的补丁安装包,一般只有付费用户才能获取到该安装包,其他用户获取不到,那怎么办呢? 万能的互联网可以办到,可以用http://www.wangpansou.cn/所有其他用 ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- eclispse快捷操作
1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索对话框:Ctrl+H快速O ...
- Hadoop之回收站
一.回收站简介: 在HDFS里,删除文件时,不会真正的删除,其实是放入回收站/trash,回收站里的文件可以快速恢复. 可以设置一个时间阀值,当回收站里文件的存放时间超过这个阀值或是回收站被清空时,文 ...
- nginx完美支持tp框架
nginx完美支持tp框架 server { listen 80; server_name mit.520m.com.cn; access_log /data/wwwlogs/mit.520m.com ...
- JQuery知识快览之五—操作属性和结构
前文提到JQuery提供了丰富的内置方法来操作对象集,本文主要介绍JQuery中的那些内置操作属性和DOM结构的方法. prop还是attr? 从JQuery1.6版开始,新增了prop方法来获取和设 ...
- 20150612_Andriod contextual action mode 菜单
参考地址:http://www.xuebuyuan.com/1114028.html http://www.cnblogs.com/mengdd/p/3564782.html ...
- poj1811 Prime Test
http://poj.org/problem?id=1811 #include <cstdio> #include <cstring> #include <algorit ...