webservice-实现手机号码归属地的查询

package com.webservice;

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

/**
 * get方法获取手机归属地
 * @author zhunode
 *
 */
public class mobilephone {
	public void getPhone(String mobileCode,String userID) throws Exception{
        URL url =new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+"&userID="+userID);
        HttpURLConnection counc = (HttpURLConnection)url.openConnection();
        counc.setRequestMethod("GET");
        if(counc.getResponseCode()==HttpURLConnection.HTTP_OK) {
        	InputStream is = counc.getInputStream();
        	ByteArrayOutputStream boas = new ByteArrayOutputStream();
        	byte[] buffer = new byte[1024];
        	int len;
        	 if((len=is.read(buffer)) != -1){
                 boas.write(buffer, 0, len);
             }
             System.out.println("GET请求获取的数据:\n"+boas.toString());  
             boas.close();
             is.close();
             
        }
	}
	public void postPhoneInfo() {
		
	}
	
	public static void main(String[] args) throws Exception {
		mobilephone mobile = new mobilephone();
		mobile.getPhone("18150850130", "");
	}
}

输出结果为:

GET请求获取的数据:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">18150850130:福建 宁德 福建电信CDMA卡</string>

原文章链接:webservice-实现手机号码归属地的查询

猜你喜欢

转载自blog.csdn.net/zuoyouzouzou/article/details/85333676