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 ...
随机推荐
- http-使用get和post方式提交数据
注意点: 1.Get和post这两种提交方式有何不同? 很明显post方式提交多了content-length和content-type这两项,所以post提交是要为这两项设置setRequestPr ...
- 160223、jquery中attr和prop的区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- sql回滚
rollback是针对事务的,你如果没有在执行语句之前开启事务,那么无法rollback,建议你还是想别的办法吧,事务语句如下(sqlserver的给你借鉴):--开启事务begin tran --执 ...
- C# 探索c#之Async、Await剖析
探索c#之Async.Await剖析 作者:蘑菇先生 出处:http://mushroom.cnblogs.com/
- zjtd 2016面试
1.写一个函数get_next() class A{ public :int next(); //取下一个值,并且指针后移 bool has_next(); private: //可以认为是一个q ...
- [HTML]JS添加表格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】java_web开发入门
转载地址:http://www.cnblogs.com/xiaoqv/archive/2011/12/10/2283348.html 一.Tomcat服务器常见启动问题:(1).Java_home环境 ...
- maven 手动安装jar到仓库的命令
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.6.0 -Dpackaging=jar -Dfile ...
- 移动端web出现的一系列问题
今天做移动端的web,在做后期处理的时候,发现了非常多的问题.下面我分别列举一下吧~~ 1.移动端浏览器众多,各种浏览器之间的显示等都有差异,很多需要单独处理,于是我需要判断分别是什么浏览器.js代码 ...
- UPnP基本原理介绍
http://blog.csdn.net/braddoris/article/details/41576515 随着计算机产业以及计算机网络技术的迅猛发展,越来越多嵌入式设备的出现和家庭网络的发展,实 ...