渔人码头

天行健,君子以自强不息。地势坤,君子以厚德载物。
posts - 12, comments - 16, trackbacks - 0, articles - 43
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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

Posted on 2006-12-11 15:55 Fisher 阅读(10007) 评论(10)  编辑  收藏 所属分类: Java应用

在用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 的快。


评论

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2006-12-11 16:08 by Fisher
转载一些代码,使用HttpUrlConnection来模拟ie form登陆web:


关于java模拟ie form登陆web的问题

HttpURLConnection urlConn=(HttpURLConnection)(new URL(url).openConnection());
urlConn.addRequestProperty("Cookie",cookie);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn.setFollowRedirects(true);
urlConn.setDoOutput(true); // 需要向服务器写数据
urlConn.setDoInput(true); //
urlConn.setUseCaches(false); // 获得服务器最新的信息
urlConn.setAllowUserInteraction(false);
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-Language","en-US" );
urlConn.setRequestProperty("Content-Length", ""+data.length());

DataOutputStream outStream = new DataOutputStream(urlConn.getOutputStream());
outStream.writeBytes(data);
outStream.flush();
outStream.close();

cookie=urlConn.getHeaderField("Set-Cookie");
BufferedReader br=new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"gb2312"));


# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2007-04-09 17:03 by dongle
好文,解决我的大问题了

# 这样真的能解决问题吗?  回复  更多评论   

2007-05-31 22:12 by Rachel
我写了段提取网页内容的程序,批量访问此网站下的明细网页内容并抓取(http://cn.made-in-china.com

测试时执行没问题
执行到几十次后,返回都是空
再后来一次都不灵了
访问URL的代码跟你写的几乎一样
获取的是以下结果


<p>Due to network security, your access to Made-in-China.com
has been temporarily denied.</p>
<p>In order to provide you with safe and stable web services,
we have to prevent abuse of Made-in-China.com by implementing
additional security measures. We hope you understand
and cooperate with us.</p>

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2007-05-31 22:35 by Rachel
最后
拔掉router
再插上
解析正常 :-)

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2007-07-04 16:51 by smalltiger
非常感谢你的这篇目文章!帮了我的大忙了,想和你交个朋友,可以的话请加我的Q:109030035或者MSN:109030035@qq.com

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2008-02-25 10:00 by Fisher
好久没有搞Java了,想不到这么多朋友看了我的帖子,呵呵
很高兴能帮到楼上的那个朋友。

最近我发现有个叫网络爬虫的开源组建那些,应该会比我这个办法好

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2008-02-26 22:51 by qyxxpd.com
@Rachel
我写了段提取网页内容的程序,批量访问此网站下的明细网页内容并抓取(http://cn.made-in-china.com

其实你用.MainWebFetcher.DownLoadPages("http://cn.made-in-china.com/", "C://tmp//test.txt");

http://cn.made-in-china.com后加/就行了.

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2008-05-08 09:43 by abyer
你这个如何验证用户名和密码啊

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2011-03-09 09:27 by whs
你这是模拟IE吗?你是模拟火狐好不?标题都搞错

# re: 关于java模拟ie 访问web网站的解决方法  回复  更多评论   

2011-08-26 11:57 by noname
傻子,别看到一个Mozilla/4.0就以为是火狐,半吊子好好学着,别出来丢人现眼。@whs

只有注册用户登录后才能发表评论。


网站导航: