直接调用http接口
//用到的相关包
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public void dosomething(String data){
//以淘宝的公共接口测试
URL url = new URL("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号");
//打开和url之间的连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
//设置通用的请求属性
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
//设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,发送post请求必须设置
conn.setDoOutput(true);
conn.setDoInput(true);
//获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
//发送请求参数即数据
out.print(data.toString());
//缓冲数据
out.flush();
//获取URLConnection对象对应的输入流
InputStream is = conn.getInputStream();
//构造一个字符流缓存
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
System.out.println(str);
}
//关闭流
is.close();
//断开连接
conn.disconnect();
}