您的位置:首页 > 理论基础 > 计算机网络

httpclient小例子:获取手机号地址、IP地址获取、POST方式注册用户(乐蜂网)

2012-09-26 17:53 525 查看
一、HttpClient注册网站用户,拿乐峰网为例,感觉是个不错的网站。我想去面试怕不要我啊。。。

测试可以注册成功,而且参数如果把某个参数都写上验证码功能也就无效了~测试可以连续注册成功。。

(发现我们以前做的网站的验证码也成了摆设。。犯了一样的错),

httpclient包下载地址



/**
*
* @title TestHttpClient.java
* @description  测试注册乐蜂网
* @package com.test
* @author fairyhawk
* @date 2012-9-26下午05:43:56
*/

HttpClient client = new HttpClient();
PostMethod method = new PostMethod();
method.setPath("http://passport.lefeng.com/*.jsp");//
try {
NameValuePair[] data = {
new NameValuePair("usrInput", "***@163.com"),//用户名
new NameValuePair("pwdInput", "111111"),//密码
new NameValuePair("repwdInput", "111111"),//确认密码
new NameValuePair("veriInput", "xx7b")//验证码
//此处省略部分参数
};

method.setRequestBody(data);
client.executeMethod(method);
BufferedReader in = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
method.releaseConnection();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

二、获取手机号码所在地,这个第一个是用淘宝的数据,第2个是shouji.com的。shouji.com项目里的话先用淘宝的吧。

/**
* 根据手机号查询所在地,只返回Province
* taobao.com数据
* @param tel
* @return CityName
*/
@SuppressWarnings("deprecation")
public static String getCityNameByTel_taobao(String tel) {
try {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod();
method
.setPath("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="
+ tel);
client.executeMethod(method);
BufferedReader in = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream(), "GB2312"));
StringBuilder _str = new StringBuilder();
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
_str.append(inputLine);
}
method.releaseConnection();
String js = _str.toString();
JSONObject jo = JSONObject.fromObject(js.substring(_str.indexOf("{"))
.trim());
String province = jo.get("province") == null ? "" : URLDecoder
.decode(jo.get("province").toString());
//          String catName = jo.get("catName") == null ? "" : URLDecoder
//                  .decode(jo.get("catName").toString());
//          return (province.equals("") || province.equals(catName)) ? catName
//                  : province + " " + catName;
return province.equals("") ? null :province;
} catch (Exception e) {
return null;
}
}

/**
* 根据手机号查询所在地,只返回Province
* showji.com数据
* @param tel
* @return
*/
@SuppressWarnings("deprecation")
public static String getCityNameByTel_shouji(String tel) {
try {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod();
method.setPath("http://api.showji.com/Locating/20080808.aspx?m="
+ tel + "&output=json&callback=querycallback" + tel);
client.executeMethod(method);
BufferedReader in = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream(), "UTF-8"));
StringBuilder _str = new StringBuilder();
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
_str.append(inputLine).append("\n");
}
method.releaseConnection();
String js = _str.toString();
String js2 = js.substring(14 + tel.length(), js.length() - 3);
JSONObject jo = JSONObject.fromObject(js2);
String province = jo.get("Province") == null ? "" : URLDecoder
.decode(jo.get("Province").toString());
//          String city = jo.get("City") == null ? "" : URLDecoder.decode(jo
//                  .get("City").toString());
//          return (province.equals("") || province.equals(city)) ? city
//                  : province + " " + city;
return province.equals("") ? null :province;
} catch (Exception e) {
return null;
}
}


三、获取IP所在地址,新浪的接口。

/**
* 根据ip获得所在城市
*  北京
*  安徽 马鞍山
* @param ip:192.168.0.1
* @return
*/
@SuppressWarnings("deprecation")
public static String getIpAdressNameByIP(String ip) {
try {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod();
method.setPath("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ ip);
client.executeMethod(method);
BufferedReader in = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream(), "UTF-8"));
StringBuilder _str = new StringBuilder();
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
_str.append(inputLine);
}
method.releaseConnection();
String js = _str.toString();
js=js.substring(_str.indexOf("{"),js.length()-1);
JSONObject jo = JSONObject.fromObject(js);
String province = jo.get("province") == null ? "" : URLDecoder
.decode(jo.get("province").toString());
//          String city = jo.get("city") == null ? "" : URLDecoder.decode(jo
//                  .get("city").toString());
//          return (province.equals("") || province.equals(city)) ? city
//                  : province + " " + city;
return province.equals("") ? null :province;
} catch (Exception e) {
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: