httpclient小例子:获取手机号地址、IP地址获取、POST方式注册用户(乐蜂网)
2012-09-26 17:53
525 查看
一、HttpClient注册网站用户,拿乐峰网为例,感觉是个不错的网站。我想去面试怕不要我啊。。。
测试可以注册成功,而且参数如果把某个参数都写上验证码功能也就无效了~测试可以连续注册成功。。
(发现我们以前做的网站的验证码也成了摆设。。犯了一样的错),
httpclient包下载地址
二、获取手机号码所在地,这个第一个是用淘宝的数据,第2个是shouji.com的。shouji.com项目里的话先用淘宝的吧。
三、获取IP所在地址,新浪的接口。
测试可以注册成功,而且参数如果把某个参数都写上验证码功能也就无效了~测试可以连续注册成功。。
(发现我们以前做的网站的验证码也成了摆设。。犯了一样的错),
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; } }
相关文章推荐
- HttpUrlConnection和HttpClient两种方式获取GET和POST请求
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- 利用HttpClient的POST方式发起带参数的请求时,点击注册按钮无反应状态分析
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- httpclient post方式服务端获取不到数据的解决办法
- HttpClient 4 使用POST方式提交普通表单数据的例子
- ASI 实现注册方法的小例子(get和post方式)
- http post方式获取json参数
- http协议2---GET方式和POST方式获取表单数据举例 1(非统一方式获取数据,比较麻烦)
- JAVA 爬虫之httpclient post请求提交表单获取Ajax数据
- [置顶] HttpClient通过Post方式发送Json数据
- HttpClient_Post方式提交json字符串
- Java 通过HttpClient Post方式提交json,并从服务端返回json数据
- Java 通过HttpRequest获取请求用户真实IP地址
- httpclient 批量注册用户
- android httpclient处理用户登录例子 广州JAVA培训
- Android HttpClient框架get和post方式提交数据(非原创)
- 使用Nginx代理通过HttpServletRequest获取用户IP地址
- 使用 OKHttpClient的POST方式
- HttpClient post请求调取接口 例子