httpclient(get)+jsoup获取身份证归属地信息
2014-07-13 22:28
204 查看
由于最近比较忙时间有限,所以只将代码贴出来.
有时间我会将代码详细注释下.如果有不明白的请留言
其中测试的网站是
http://qq.ip138.com/idsearch/
测试的身份证号码:652302197607051677
Java代码:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Scanner;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class IdCard {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建httpclient客户端
HttpClient client=new HttpClient();
//设置client主机地址和端口
client.getHostConfiguration().setHost( "qq.ip138.com" , 80, "http" );
String url="/idsearch/index.asp";
Scanner in = new Scanner(System.in);
System.out.print("身份证号码为:");
String ID=in.next();
System.out.print(ID);
String params = "action=idcard&"+"userid="+ID+"&B1=%B2%E9+%D1%AF";
HttpMethod method = getMethod(url, params);
client.executeMethod(method);
//String response = method.getResponseBodyAsString();
String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));
System.out.println(response);
Document doc=Jsoup.parse(response);
//Elements 是 Element 的集合类
Elements element=doc.select("table"); //从加载的信息中查找table 标签
//从查找到table属性的Elements集合中获取标签 tr 或者tr[class$=alt] 表示 tr标签内class属性=alt
// Elements titleName=element.select("tr[class$=alt]");
Elements titleName=element.select("tr");
for(org.jsoup.nodes.Element name:titleName){
System.out.println(name.text());
}
}
private static HttpMethod getMethod(String url,String params) throws IOException{
GetMethod get = new GetMethod(url+"?"+params);
get.releaseConnection();
return get;
}
}
结果截图如下:还有一些小bug,没时间调,等有时间再改吧.
![](https://img-blog.csdn.net/20140713223107531?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzAxODcyMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
转载请注明作者小刘
有时间我会将代码详细注释下.如果有不明白的请留言
其中测试的网站是
http://qq.ip138.com/idsearch/
测试的身份证号码:652302197607051677
Java代码:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Scanner;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class IdCard {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建httpclient客户端
HttpClient client=new HttpClient();
//设置client主机地址和端口
client.getHostConfiguration().setHost( "qq.ip138.com" , 80, "http" );
String url="/idsearch/index.asp";
Scanner in = new Scanner(System.in);
System.out.print("身份证号码为:");
String ID=in.next();
System.out.print(ID);
String params = "action=idcard&"+"userid="+ID+"&B1=%B2%E9+%D1%AF";
HttpMethod method = getMethod(url, params);
client.executeMethod(method);
//String response = method.getResponseBodyAsString();
String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));
System.out.println(response);
Document doc=Jsoup.parse(response);
//Elements 是 Element 的集合类
Elements element=doc.select("table"); //从加载的信息中查找table 标签
//从查找到table属性的Elements集合中获取标签 tr 或者tr[class$=alt] 表示 tr标签内class属性=alt
// Elements titleName=element.select("tr[class$=alt]");
Elements titleName=element.select("tr");
for(org.jsoup.nodes.Element name:titleName){
System.out.println(name.text());
}
}
private static HttpMethod getMethod(String url,String params) throws IOException{
GetMethod get = new GetMethod(url+"?"+params);
get.releaseConnection();
return get;
}
}
结果截图如下:还有一些小bug,没时间调,等有时间再改吧.
转载请注明作者小刘
相关文章推荐
- HttpClient 发送Post,Get请求例子(包含设置请求头信息和获取返回头信息)
- 接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
- Android 如何用HttpClient 以Get方式获取数据并添加http头信息
- 第9个HttpClient 例子,HttpClient+jsoup 扩展获取网站信息
- 第8个HttpClient 例子.HttpClient+jsoup 获取网站相关信息
- 获取表维护信息的BSFN(Get Audit Information)
- 获取 GetLastError 文本信息
- WinAPI: GetSystemMetrics - 获取系统度量等数值信息
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过
- WinAPI: GetKeyboardType - 获取键盘的类型信息
- 用HttpClient模拟登录网站二(获取网页信息)
- VC中GetLastError()获取错误信息的使用
- WinAPI: GetCharABCWidths - 获取 TrueType 字符的宽度信息
- 利用WinAPI(GetFileVersionInfo)获取文件版本信息
- 【转】用GetAdaptersInfo获取网卡信息
- GetAdaptersInfo获取网卡详细信息
- NET_DVR_GetDVRConfig 获取设备配置信息的两种方式
- WinAPI: GetSystemInfo - 获取系统信息
- WinAPI: GetDiskFreeSpaceEx - 获取磁盘容量信息
- 【原】获取数据库(SQL SERVER 2005)的所有信息 Get all database information from SQL Server 2005 测试通过【想写基于数据库代码生成器可参考】