调用第三方的WS服务

调用第三方的WS服务(www.webxml.com.cn)

  1. 体验webservice,浏览www.webxml.com.cn
  2. 准备客户端,调用第三方的webservice服务,让本项目有手机查号的功能(三种方式)            
public class MobileCodeService {
	//1. http-get方式访问webservice
	public void get(String mobileCode ,String userID ) throws Exception{
		URL url=new URL("http://webservice.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://webservice.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://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
		//3.设置请求参数
          postMethod.setRequestBody(new FileInputStream("c:/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("1886594****", "");
		ws.post("1886594****", "");
	    ws.soap();

	}

}

               3、通过wsImport生成本地代理开发:

如果每次都去访问网址去使用服务无疑耗费时间,这样就有生成本地代理这个功能来解决这个问题。即通过wsimport命令。


这样桌面上就会生成如文件:即已生成本地代理


把包考到myEclipse,就可使用了

package cn.com.webxml;

import java.util.List;

public class Test {
	 /**通过wsimport生成本地代理 来访问 webservice服务端
	   * 
	   * @param args
	   */
		public static void main(String[] args) {
			//生成服务对象
			MobileCodeWS ws=new MobileCodeWS();
			//取得webservice服务的访问方式  : Soap1.1  Soap 1.2  Http-get http-Post
			MobileCodeWSSoap mobileCodeWSSoap = ws.getMobileCodeWSSoap();
			/**
			 *  返回的数据有两种类型 : 
			 *  1. 简单的数据类型  。基本数据类型 :整数、布尔、字符串 等
			 *  2. 复合的数据类型 :结构体 ,数组 ,对象 
			 */
			//1.简单的数据
			String result=mobileCodeWSSoap.getMobileCodeInfo("18865947337", "");
			System.out.println("返回的结果:"+result);
			//2. 复合的数据  List<String> List<Student>
			ArrayOfString databaseInfo = mobileCodeWSSoap.getDatabaseInfo();
			List<String> results=databaseInfo.getString();
			//遍历集合
			for(String temp:results){
				System.out.println(temp);
			}
			//jsp  Select 
		}
}

结果:

这样通过本地代理获取服务就结束了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值