关于网络爬虫httpUrlConn.getResponseCode()取不到值得问题
今天有个人加我为好友,莫名其妙的就问我有没有整过网络爬虫,其实我网络学的不是很好,就说了不会,爱着面子又说我给他问问,我有个同事以前好像搞过,结果页没找到人,就硬着头皮看了下就几行代码:
- try {
- URL pageUrl = new URL("http://www.baidu.com/");
- HttpURLConnection httpUrlConn = (HttpURLConnection) pageUrl
- .openConnection();
- int statusCode = httpUrlConn.getResponseCode();
- System.out.println(statusCode);
- } catch (Exception e) {
- e.printStackTrace();
- }
网上找了点资料看了看,上面说,这个不能再main这个主线程中跑,要另起一个线程来运行。
于是我就试了下:
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class TestUrl {
- public static void main(String[] args) {
- Test t = new Test();
- Thread demo = new Thread(t);
- demo.start();
- }
- }
- class Test implements Runnable {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- URL pageUrl = new URL("http://www.baidu.com/");
- HttpURLConnection httpUrlConn = (HttpURLConnection) pageUrl
- .openConnection();
- int statusCode = httpUrlConn.getResponseCode();
- System.out.println(statusCode);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
果然没问题了。这里记录下,下次有时间学习一下网络爬虫
api9 以后的sdk ,访问internet资源需要另开线程(大致意思,没细查)。