webservice 调用第三方服务基础练习

本文介绍了一个使用Java实现的移动客户端示例,该客户端通过HTTP GET请求从远程服务器获取手机验证信息。示例代码展示了如何创建URL对象、打开HTTP连接、设置请求参数、读取响应数据并解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package mobile.cli;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import sun.nio.cs.ext.ISCII91;

import com.sun.swing.internal.plaf.basic.resources.basic;
import com.sun.xml.internal.bind.v2.model.core.ID;

public class MobileClient {

	/**
	 * @param args
	 */
	 
	//get 方法方法获取网站发布的第三方服务的原始操作。
	public void getmobile(String mobileCode ,String userID) throws Exception{
		//1:获取url 2:打开连接 3:设置请求方法和超时时间4:读取输入流中的数据5:关闭资源
		
		//1:这里的url是通过网站上获取的
		
		URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?" +
				"mobileCode="+mobileCode+"&userID="
				+userID);
		//2:打开连接
		HttpURLConnection connection=(HttpURLConnection) url.openConnection();
		//3:设置超时时间和请求方式
		connection.setConnectTimeout(50000);
		connection.setRequestMethod("GET");
		//4:读取数据进行处理
		if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
			//读取连接获取的输入流的数据
     	  InputStream is =connection.getInputStream();
     	     //定义数组和输出流镜输入流的的字符写入数组
     	  int len=-1;
     	  byte[] buffer=new byte[1024];
     	  ByteArrayOutputStream bas=new ByteArrayOutputStream();
     	    while((len=is.read(buffer))!=-1){
     	    	//输出流写入
     	    	bas.write(buffer, 0, len);
     	    }
     	     System.out.println(bas.toString());
     	bas.close();
     	is.close();
		}
	
	}
	
	public static void main(String[] args) throws Exception {
		MobileClient mb=new MobileClient();
		mb.getmobile("1329817", "");

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值