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", "");
}
}
webservice 调用第三方服务基础练习
最新推荐文章于 2020-08-11 15:20:30 发布