Java模拟post获取信息

很好很强大的 2012-02-09 04:59:11
试了很多次就是查不到信息,求高手看看!!!

网址:http://gaj.baoji.gov.cn/gongancar/index.asp
用例:陕C11540

我的代码:

public static String getPost(String str_url){
try {
URL url = new URL(str_url);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setInstanceFollowRedirects(true);
urlConn.connect();
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
String content = "cbh=陕C11540";
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine = null;
while((inputLine = reader.readLine()) != null){
System.out.println(inputLine);
}
reader.close();
urlConn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
...全文
230 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
安心逍遥 2012-02-10
  • 打赏
  • 举报
回复
IE截下了原本生成的cookie,加上就有数据
beiouwolf 2012-02-10
  • 打赏
  • 举报
回复
测出来了,这个傻X 2呆网站,必须把cookie写在head头上
加上这句
我用IE截下了原本生成的cookie,加上就有数据了
con.setRequestProperty("Cookie", "ASPSESSIONIDSADDADDB=ALKIHJNBKLFFGEOFPJPKAMEH");

应该是服务器上做session认证了
不太清楚asp的sessionid生成机制,你想办法能生成对应的sessionid应该就能访问了
很好很强大的 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ilovefzq 的回复:]

经过我的测试,你的代码模拟post是很成功的,我自己的网站是可以接收到参数的,但是会产生中文乱码,乱码这个东东是需要服务器协调的,所以http://gaj.baoji.gov.cn/gongancar/index.asp
有可能接收到的是乱码,
还有一种情况我无法理解就是http://gaj.baoji.gov.cn/gongancar/find.asp?cbh=陕C,也是查不到数据的,超越……
[/Quote]
呵呵 有可能是服务器端做了屏蔽之类的吧
很好很强大的 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 beiouwolf 的回复:]

Java code

try {
URL dataUrl = new URL("http://gaj.baoji.gov.cn/gongancar/find.asp");
HttpURLConnection con = (HttpURLConnection) dataUrl
.openConnection(……
[/Quote]
没有获取到: 尚无您的违章信息!
用浏览器是可以查询到违章信息的
ilovefzq 2012-02-09
  • 打赏
  • 举报
回复
综上所诉,这个网站不是我们可以利用的,放弃吧
ilovefzq 2012-02-09
  • 打赏
  • 举报
回复
经过我的测试,你的代码模拟post是很成功的,我自己的网站是可以接收到参数的,但是会产生中文乱码,乱码这个东东是需要服务器协调的,所以http://gaj.baoji.gov.cn/gongancar/index.asp
有可能接收到的是乱码,
还有一种情况我无法理解就是http://gaj.baoji.gov.cn/gongancar/find.asp?cbh=陕C,也是查不到数据的,超越了我的理解范围
beiouwolf 2012-02-09
  • 打赏
  • 举报
回复

try {
URL dataUrl = new URL("http://gaj.baoji.gov.cn/gongancar/find.asp");
HttpURLConnection con = (HttpURLConnection) dataUrl
.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);

OutputStream os = con.getOutputStream();
os.write("cbh=陕C11540".getBytes());
os.flush();
os.close();

InputStream is = con.getInputStream();
byte d[] = new byte[1024];
int size = 0;
while ((size = is.read(d)) != -1)
System.out.println(new String(d, 0, size));

con.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}


结果



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.style2 { font-size: 16px;
font-weight: bold;
}
.style4 {font-size: 16px; font-weight: bold; color: #FF0000; }
.style5 {color: #FFFFFF}
.style6 {color: #FF0000}
-->
</style></head>

<body>

<div align="center"><br />
<span class="style2"><br />
<br />
<br />
<span class="style6">尚无您的违章信息!</span></span><span class="style6"><br />
</span>
</div>

</body>
</html>


很好很强大的 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 beiouwolf 的回复:]

http://gaj.baoji.gov.cn/gongancar/index.asp
哎...自己拿ie打开了看看...查询那个按钮的地址是
http://gaj.baoji.gov.cn/gongancar/find.asp
[/Quote]

哦 帖子上面是我写错了
是这个http://gaj.baoji.gov.cn/gongancar/find.asp
你试试这个能获取到么?
beiouwolf 2012-02-09
  • 打赏
  • 举报
回复
http://gaj.baoji.gov.cn/gongancar/index.asp
哎...自己拿ie打开了看看...查询那个按钮的地址是
http://gaj.baoji.gov.cn/gongancar/find.asp

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧