关于java模拟ie 访问web网站的解决方法

本文详细介绍了使用Java的HttpURLConnection下载网页时遇到的Google网站访问限制问题,并提供了完整的代码示例。同时,推荐了另一种更有效的访问方法——使用Apache HttpClient。最后,对比了两种方法的性能,建议使用HttpURLConnection,因其更为轻量级且速度快。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在用Java的HttpURLConnection 来下载网页,发现访问google的网站时,会被google拒绝掉。

try
{
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);

// logger.info(httpConn.getResponseMessage());
in = httpConn.getInputStream();
out = new FileOutputStream(new File(outPath));

chByte = in.read();
while (chByte != -1)
{
out.write(chByte);
chByte = in.read();
}
}
catch (MalformedURLException e)
{
}
}


经过一段时间的研究和查找资料,发现是由于上面的代码缺少了一些必要的信息导致,增加更加详细的属性

httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

完整代码如下:
public static void DownLoadPages(String urlStr, String outPath)
{
int chByte = 0;
URL url = null;
HttpURLConnection httpConn = null;
InputStream in = null;
FileOutputStream out = null;

try
{
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// logger.info(httpConn.getResponseMessage());
in = httpConn.getInputStream();
out = new FileOutputStream(new File(outPath));

chByte = in.read();
while (chByte != -1)
{
out.write(chByte);
chByte = in.read();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();
in.close();
httpConn.disconnect();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

此外,还有第二种方法可以访问Google的网站,就是用apache的一个工具HttpClient 模仿一个浏览器来访问Google

Document document = null;
HttpClient httpClient = new HttpClient();

GetMethod getMethod = new GetMethod(url);
getMethod.setFollowRedirects(true);
int statusCode = httpClient.executeMethod(getMethod);

if (statusCode == HttpStatus.SC_OK)
{
InputStream in = getMethod.getResponseBodyAsStream();
InputSource is = new InputSource(in);

DOMParser domParser = new DOMParser(); //nekoHtml 将取得的网页转换成dom
domParser.parse(is);
document = domParser.getDocument();

System.out.println(getMethod.getURI());

}
return document;

推荐使用第一种方式,使用HttpConnection 比较轻量级,速度也比第二种HttpClient 的快。
Java代码编写的简单网页浏览器,仿IE基础功能,只能解析标准的HTML文件,现在流行的HTML5格式不能解析。使用方法:可直接输入文件名或网络地址,但必需事先连入网络。   JTextField jtfAddress; //输入html文件地址或网址   JButton jbGo; //转到文件按钮   JTextPane jtpShow; //显示文件   JLabel jlInfo; //提示信息   public HTTPBrowserDemo(){    super("html浏览器"); //调用父类构造函数    jtfAddress=new JTextField(20); //实例化地址输入框    jbGo=new JButton("转到"); //实例化转向按钮    jtpShow=new JTextPane(); //实例化显示内容框    jlInfo=new JLabel(); //实例化信息提示标签    JPanel panel=new JPanel(); //实例化面板    panel.add(new JLabel("地址")); //增加组件到面板上    panel.add(jtfAddress);    panel.add(jbGo);    JScrollPane jsp=new JScrollPane(jtpShow); //实例化滚动窗体    Container container=getContentPane(); //得到容器    container.add(panel,BorderLayout.NORTH); //增加组件到容器上    container.add(jsp,BorderLayout.CENTER);    container.add(jlInfo,BorderLayout.SOUTH);    jbGo.addActionListener(new ShowHTMLListener()); //事件处理,发生按钮点击时显示页面内容    jtfAddress.addActionListener(new ShowHTMLListener());    setSize(350,280); //设置窗口尺寸    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序   }   class ShowHTMLListener implements ActionListener { //显示页面内容事件处理    public void actionPerformed(ActionEvent event) {    try{    jlInfo.setText("正在连接..."); //显示提示信息    URL address=new URL(jtfAddress.getText()); //得到HTML页面的URL地址    jtpShow.setPage(address); //设置显示页面    jlInfo.setText("完成");    }    catch (Exception ex){    jlInfo.setText("连接出错");    ex.printStackTrace(); //输出出错信息    }    }   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值