import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod; /**调用第三方的webservice服务 ,获取电话号码信息
*
*/
public class MobileCodeService {
//1. http-get方式访问webservice
public void get(String mobileCode ,String userID ) throws Exception{
URL url=new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+
"&userID="+userID);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){ //结果码=200
InputStream is=conn.getInputStream();
//内存流 ,
ByteArrayOutputStream boas=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1){
boas.write(buffer, 0, len);
}
System.out.println("GET请求获取的数据:"+boas.toString());
boas.close();
is.close();
}
} //2.Post请求 :通过Http-Client 框架来模拟实现 Http请求
public void post(String mobileCode ,String userID) throws Exception{
/**HttpClient访问网络的实现步骤:
* 1. 准备一个请求客户端:浏览器
* 2. 准备请求方式: GET 、POST
* 3. 设置要传递的参数
* 4.执行请求
* 5. 获取结果
*/
HttpClient client=new HttpClient();
PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
//3.设置请求参数
postMethod.setParameter("mobileCode", mobileCode);
postMethod.setParameter("userID", userID);
//4.执行请求 ,结果码
int code=client.executeMethod(postMethod);
//5. 获取结果
String result=postMethod.getResponseBodyAsString();
System.out.println("Post请求的结果:"+result);
}
//2.Post请求 :通过Http-Client 框架来模拟实现 Http请求
public void soap() throws Exception{ HttpClient client=new HttpClient();
PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//3.设置请求参数
postMethod.setRequestBody(new FileInputStream("D:/soap.xml"));
//修改请求的头部
postMethod.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
//4.执行请求 ,结果码
int code=client.executeMethod(postMethod);
System.out.println("结果码:"+code);
//5. 获取结果
String result=postMethod.getResponseBodyAsString();
System.out.println("Post请求的结果:"+result);
} public static void main(String[] args) throws Exception{
MobileCodeService ws=new MobileCodeService();
ws.get("15958083603", "");
ws.post("15958012349", "");
ws.soap(); } }

WebService Get/Post/Soap 方式请求的更多相关文章

  1. 最简单易懂的webService客户端之soap+xml请求

    代码准备: 1.网络上有提供一些免费的服务器测试地址,可以上这里找一找:https://my.oschina.net/CraneHe/blog/183471 2.我选择了一个翻译地址:http://w ...

  2. java使用POST发送soap报文请求webservice返回500错误解析

    本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...

  3. C#使用Http的Post方式请求webservice

    webservice是以前比较流行的跨系统.跨语言.跨平台的数据交互技术.最近工作中调用Java作为服务端开放的webser,我是通过VS205生成webservice工具类的方式进行接口调用的.用这 ...

  4. Node.js 使用 soap 模块请求 WebService 服务接口

    项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求. 使用SOAP请求webservice服务的流程如下: 1.进入项目目录,安装 so ...

  5. [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现

    本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...

  6. .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox

    一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...

  7. 调用webService的几种方式

    转自:http://blog.csdn.net/u011165335/article/details/51345224 一.概览 方式1: HttpClient:可以用来调用webservie服务,也 ...

  8. WebService发布协议--SOAP和REST的区别

    HTTP是标准超文本传输协议.使用对参数进行编码并将参数作为键值对传递,还使用关联的请求语义.每个协议都包含一系列HTTP请求标头及其他一些信息,定义客户端向服务器请求哪些内容,服务器用一系列HTTP ...

  9. Atitit 动态调用webservice与客户端代理方式调用

    Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke  直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...

随机推荐

  1. SQLServer数据库查看死锁、堵塞情况

    在压力测试过程中,不间断的按F5键执行上面的SQL语句,如果出现死锁或者堵塞现象,就会在执行结果中罗列出来.如果每次连续执行SQL,都有死锁或者堵塞出现,说明死锁或者堵塞的比较严重. --每秒死锁数量 ...

  2. HDU 3516 DP 四边形不等式优化 Tree Construction

    设d(i, j)为连通第i个点到第j个点的树的最小长度,则有状态转移方程: d(i, j) = min{ d(i, k) + d(k + 1, j) + p[k].y - p[j].y + p[k+1 ...

  3. Puppet 安装配置

    环境说明: OS:CentOS 5.4 i386 puppetmaster    192.168.0.12    hostname: puppetmaster.info.com client      ...

  4. 开始 python programming第三版案例分析

    最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...

  5. HDU 3341 Lost's revenge

    Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ...

  6. Unity3D for iOS初级教程:Part 3/3

    转自Unity 3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这份教程是由教程团队成员 ...

  7. UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree

    魔法森林高清重置, 只需要维护关于t的最大生成树,然后链上边权求和即可. 直接上LCT 调了将近2h 吃枣药丸 #include <cstdio> #include <cstring ...

  8. BZOJ 1226 [SDOI2009]学校食堂Dining ——状压DP

    看到B<=8,直接状态压缩即可. dp[i][j][k]表示当前相对位置是关于i的,并且i以前的已经就餐完毕,j表示i和之后的就餐情况,k表示上一个就餐的人的相对位置. 然后Dp即可 #incl ...

  9. hdu 2859

    #include<stdio.h> char s[1010][1010]; int map[1010][1010]; int main() {  int n,i,j,k,ii,jj;  w ...

  10. SQL Server中的@@ROWCOUNT

    SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...