用java实现百度搜索返回搜索后的页面html的程序 大神看看哪里错了

对方vsDVD顺丰V大说 2014-02-12 09:19:02
比如百度搜索“23” 可以用www.baidu.com/s?wd=23来访问
下面的程序是往www.baidu.com/s用post的方式提交wd=23
但是返回的页面是“对不起,您访问的页面不存在”。求大神看看哪里错了。
import java.io.*;
import java.util.*;
import java.net.*;

public class sdsw {public static void main(String[] args) {

/**
* 向指定URL发送POST方法的请求
*
* @param url
* 发送请求的URL
* @param params
* 请求参数,请求参数应该是name1=value1&name2=value2的形式。
* @return URL所代表远程资源的响应
*/
String url="http://www.baidu.com/s";String params="wd=23";
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();

// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
System.out.println(line);
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}
...全文
537 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
帖子都不结的啊。
求大神指导。
tony4geek 2014-02-13
  • 打赏
  • 举报
回复
帖子都不结的啊。
  • 打赏
  • 举报
回复
引用 12 楼 song_1990 的回复:
其实百度最终是post提交的,但是在js脚本上做了处理,你企图用程序写的post提交是不成立的~也就是请求结果不正确。
多谢。 我刚刚试了试http://www.8u.cn/members/login.asp这个网站。这个网站的登陆表格是post的,但是我的程序也得不到登陆后的界面。 import java.io.*; import java.util.*; import java.net.*; public class sdsw {public static void main(String[] args) { /** * 向指定URL发送POST方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=value1&name2=value2的形式。 * @return URL所代表远程资源的响应 */ String url="http://www.8u.cn/members/login.asp";String params="usrname=todayfriday&usrpass=zxdzxd"; PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(params); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; System.out.println(line); } } catch (Exception e) { System.out.println("发送POST请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
song_wl 2014-02-13
  • 打赏
  • 举报
回复
其实百度最终是post提交的,但是在js脚本上做了处理,你企图用程序写的post提交是不成立的~也就是请求结果不正确。
  • 打赏
  • 举报
回复
引用 9 楼 song_1990 的回复:
………………你以post提交,在百度服务器的请求中是404。哪里有返回?
多谢大神。也就是说只能用url来做是吗?
song_wl 2014-02-13
  • 打赏
  • 举报
回复
………………你以post提交,在百度服务器的请求中是404。哪里有返回?
  • 打赏
  • 举报
回复
引用 7 楼 song_1990 的回复:
那你是把你的请求给了百度服务器取出来,但是百度服务器搜索的时候是get啊,你能改?
浏览器百度搜索的时候 最后不是会显示搜索结果的页面吗? 现在就是想拿到服务器返回回来的这个页面的html。
song_wl 2014-02-13
  • 打赏
  • 举报
回复
那你是把你的请求给了百度服务器取出来,但是百度服务器搜索的时候是get啊,你能改?
  • 打赏
  • 举报
回复
引用 5 楼 song_1990 的回复:
你这是要post提交然后访问百度搜索吗?
是的。
song_wl 2014-02-13
  • 打赏
  • 举报
回复
你这是要post提交然后访问百度搜索吗?
  • 打赏
  • 举报
回复
引用 3 楼 rui888 的回复:
  URL url = new URL("http://www.baidu.com/s?ie=utf-8&bs=23&f=8&rsv_bp=1&wd=23&rsv_sug3=1&rsv_sug4=89&rsv_sug1=1&inputT=0");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET"); 
		conn.setDoOutput(true); 
		InputStream inStream = conn.getInputStream();
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		System.out.println(new String(data, "utf-8"));
多谢大神回复。但是我想问下如果不用url传值,而是用发送http请求的方式呢?就像我代码中的方法。
tony4geek 2014-02-13
  • 打赏
  • 举报
回复
  URL url = new URL("http://www.baidu.com/s?ie=utf-8&bs=23&f=8&rsv_bp=1&wd=23&rsv_sug3=1&rsv_sug4=89&rsv_sug1=1&inputT=0");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET"); 
		conn.setDoOutput(true); 
		InputStream inStream = conn.getInputStream();
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		System.out.println(new String(data, "utf-8"));

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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