调用第三方的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 
		}
}

结果:

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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 的 WebSocket API 来连接第三方 WebSocket 服务器。以下是一个简单的示例代码,可以连接到指定的 WebSocket 服务器地址: ```java import java.net.URI; import javax.websocket.*; @ClientEndpoint public class WebSocketClient { @OnOpen public void onOpen(Session session) { System.out.println("Connected to server"); } @OnMessage public void onMessage(String message) { System.out.println("Received message: " + message); } @OnError public void onError(Throwable error) { error.printStackTrace(); } @OnClose public void onClose() { System.out.println("Connection closed"); } public static void main(String[] args) { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); String uri = "ws://example.com/websocket"; try { Session session = container.connectToServer(WebSocketClient.class, URI.create(uri)); session.getBasicRemote().sendText("Hello, server!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,`@ClientEndpoint` 注解表示这个类是一个 WebSocket 客户端。`@OnOpen`、`@OnMessage`、`@OnError` 和 `@OnClose` 注解分别表示连接建立、接收到消息、发生错误和连接关闭时的回调函数。`WebSocketContainer` 类用于管理 WebSocket 连接,`ContainerProvider.getWebSocketContainer()` 方法可以获取一个默认的 WebSocketContainer 实例。`connectToServer()` 方法用于连接到指定的 WebSocket 服务器,第一个参数是客户端类的 Class 对象,第二个参数是服务器地址的 URI。在 `main()` 方法中,我们先获取了一个 WebSocketContainer 实例,然后调用 `connectToServer()` 方法连接到指定的 WebSocket 服务器,最后发送了一条消息到服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值