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. UVa 11987 并查集 Almost Union-Find

    原文戳这 与以往的并查集不同,这次需要一个删除操作.如果是叶子节点还好,直接修改父亲指针就好. 但是如果要是移动根节点,指向它的所有子节点也会跟着变化. 所以要增加一个永远不会被修改的虚拟根节点,这样 ...

  2. !!注意!部署出现the requested resource is not available

    避免项目里重复包出现 同时tomcat的lib里避免重复包,也会出现requested resource isnot available!

  3. C#Windows服务安装

    1,做好windows服务后,生成 一下,然后在项目目录中找到bin文件夹下的Debug文件夹,文件夹下有文件xxxx.exe 2,然后在C:\Windows\Microsoft.NET\Framew ...

  4. Appium解锁九宫格(TouchAction)

    TouchAction 1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.py class TouchActio ...

  5. Python第三方库之openpyxl(11)

    Python第三方库之openpyxl(11) Stock Charts(股票图) 在工作表上按特定顺序排列的列或行中的数据可以在股票图表中绘制.正如其名称所暗示的,股票图表通常被用来说明股价的波动. ...

  6. JSP标签:jsp内置标签、jstl标签、自定义标签

     一.jsp标签的分类: 1)内置标签(动作标签): 不需要在jsp页面导入标签 2)jstl标签: 需要在jsp页面中导入标签 3)自定义标签 : 开发者自行定义,需要在jsp页面导入标签    1 ...

  7. 【JavaScript 7—基础知识点】:BOM

    一.基础知识 1.1,什么是BOM BOM(browser object model):也叫浏览器对象模型,它提供了很多对象,用于访问浏览器的功能.BOM缺少规范,每个浏览器提供商又按照自己想法去扩展 ...

  8. document.execCommand

    document.execCommand 在firefox浏览器执行不好,但是在其他浏览器有时候使用会非常方便. 比如在input标签中使用: onkeyup="if(isNaN(value ...

  9. POJ 2359 Questions

    Questions Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1228   Accepted: 449 Descript ...

  10. shell的for循环

    与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...