java客户端调用webservice_Java客户端调用Webservice接口流程

展开全部

给你看看以前写的获取电话号码归属地的代码的三种方法,然后你就62616964757a686964616fe78988e69d8331333361323563懂了。import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.PostMethod;

public class MobileCodeService {

public void httpGet(String mobile,String userID) throws Exception

{

//http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string

URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobile+"&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 arrayOutputStream = new ByteArrayOutputStream();  //

byte [] buf = new byte[1024];

int len = -1;

while((len = is.read(buf))!=-1)

{

//获取结果

arrayOutputStream.write(buf, 0, len);

}

System.out.println("Get方式获取的数据是:"+arrayOutputStream.toString());

arrayOutputStream.close();

is.close();

}

}

public void httpPost(String mobile,String userID) throws HttpException, IOException

{

//访问路径   http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo

//HttpClient访问

HttpClient httpClient = new HttpClient();

PostMethod pm = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");

pm.setParameter("mobileCode", mobile);

pm.setParameter("userID", userID);

int code= httpClient.executeMethod(pm);

System.out.println("状态码:"+code);

//获取结果

String result = pm.getResponseBodyAsString();

System.out.println("获取到的数据是:"+result);

}

public void SOAP() throws Exception

{

HttpClient client = new HttpClient();

PostMethod method = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");

//设置访问方法的参数

method.setRequestBody(new FileInputStream("C:\\soap.xml"));

method.setRequestHeader("Content-Type","text/xml; charset=utf-8");

int code= client.executeMethod(method);

System.out.println("状态码:"+code);

//获取结果

String result = method.getResponseBodyAsString();

System.out.println("获取到的数据是:"+result);

}

public static void main(String[] args) throws Exception {

MobileCodeService mcs=new MobileCodeService();

mcs.httpGet("18524012513", "");

//mcs.httpPost("18524012513", "");

//mcs.SOAP();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值