HttpClient设置HTTP请求头Header

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

用Firebug对POST的数据进行监控 请求 HTTP头 信息,得到如下内容:

Java代码   收藏代码
  1. Accept  application/json, text/javascript, */*  
  2. Accept-Encoding gzip, deflate  
  3. Accept-Language en-us,en;q=0.5  
  4. Cache-Control   no-cache  
  5. Content-Length  432  
  6. Content-Type    application/x-www-form-urlencoded; charset=UTF-8  
  7. Host    www.huaxixiang.com  
  8. Pragma  no-cache  
  9. Proxy-Connection    keep-alive  
  10. Refere   http://www.huaxixiang.com/CrossPriceDetail.action  
  11. User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0  
  12. X-Requested-With    XMLHttpRequest  

用HttpClient模仿浏览器访问页面,加载URL的HTML信息,为了良好的加载网站的信息,不被限制.

为了说明请求头的信息添加了一个小测试项目LoginTest,添加页面index.jsp,添加主要代码打印Http Header的JSP页面.

主要打印Http Header信息.

1. index.jsp

Java代码   收藏代码
  1. out.println("Protocol: " + request.getProtocol());   
  2. out.println("Scheme: " + request.getScheme());   
  3. out.println("Server Name: " + request.getServerName() );   
  4. out.println("Server Port: " + request.getServerPort());   
  5. out.println("Protocol: " + request.getProtocol());   
  6. out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo());   
  7. out.println("Remote Addr: " + request.getRemoteAddr());  
  8. out.println("Remote Host: " + request.getRemoteHost());   
  9. out.println("Character Encoding: " + request.getCharacterEncoding());   
  10. out.println("Content Length: " + request.getContentLength());   
  11. out.println("Content Type: "+ request.getContentType());   
  12. out.println("Auth Type: " + request.getAuthType());   
  13. out.println("HTTP Method: " + request.getMethod());   
  14. out.println("Path Info: " + request.getPathInfo());   
  15. out.println("Path Trans: " + request.getPathTranslated());   
  16. out.println("Query String: " + request.getQueryString());   
  17. out.println("Remote User: " + request.getRemoteUser());   
  18. out.println("Session Id: " + request.getRequestedSessionId());   
  19. out.println("Request URI: " + request.getRequestURI());   
  20. out.println("Servlet Path: " + request.getServletPath());   
  21. out.println("Accept: " + request.getHeader("Accept"));   
  22. out.println("Host: " + request.getHeader("Host"));   
  23. out.println("Referer : " + request.getHeader("Referer"));   
  24. out.println("Accept-Language : " + request.getHeader("Accept-Language"));   
  25. out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));   
  26. out.println("User-Agent : " + request.getHeader("User-Agent"));   
  27. out.println("Connection : " + request.getHeader("Connection"));   
  28. out.println("Cookie : " + request.getHeader("Cookie"));   
  29. out.println("Created : " + session.getCreationTime());   
  30. out.println("LastAccessed : " + session.getLastAccessedTime());   

 

2. 使用IE浏览器加载http://127.0.0.1:8080/LoginTest/index.jsp返回内容如下:

Java代码   收藏代码
  1. Protocol: HTTP/1.1   
  2. Scheme: http   
  3. Server Name: 127.0.0.1   
  4. Server Port: 8080   
  5. Protocol: HTTP/1.1   
  6. Server Info: Apache Tomcat/6.0.18   
  7. Remote Addr: 127.0.0.1   
  8. Remote Host: 127.0.0.1   
  9. Character Encoding: null   
  10. Content Length: -1   
  11. Content Type: null   
  12. Auth Type: null   
  13. HTTP Method: GET   
  14. Path Info: null   
  15. Path Trans: null   
  16. Query String: null   
  17. Remote User: null   
  18. Session Id: E2C384C095E34AD355684EB554517FB1   
  19. Request URI: /LoginTest/index.jsp   
  20. Servlet Path: /index.jsp   
  21. Accept: */*   
  22. Host: 127.0.0.1:8080   
  23. Referer : null   
  24. Accept-Language : en-us   
  25. Accept-Encoding : gzip, deflate   
  26. User-Agent : Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.3; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)   
  27. Connection : Keep-Alive   
  28. Cookie : JSESSIONID=E2C384C095E34AD355684EB554517FB1   
  29. Created : 1322294859981   
  30. LastAccessed : 1322294859981  

 

3.  后面使用HttpClient不设置header信息加载http://127.0.0.1:8080/LoginTest/index.jsp信息如下:

Java代码   收藏代码
  1. Protocol: HTTP/1.1  
  2. Scheme: httpServer   
  3. Name: 127.0.0.1  
  4. Server Port: 8080  
  5. Protocol: HTTP/1.1  
  6. Server Info: Apache Tomcat/6.0.18  
  7. Remote Addr: 127.0.0.1  
  8. Remote Host: 127.0.0.1  
  9. Character Encoding: null  
  10. Content Length: -1  
  11. Content Type: null  
  12. Auth Type: null  
  13. HTTP Method: GET  
  14. Path Info: null  
  15. Path Trans: null  
  16. Query String: null  
  17. Remote User: null  
  18. Session Id: null  
  19. Request URI: /LoginTest/index.jspServlet   
  20. Path: /index.jsp  
  21. Accept: null  
  22. Host: 127.0.0.1:8080  
  23. Referer : null  
  24. Accept-Language : null  
  25. Accept-Encoding : null  
  26. User-Agent : Apache-HttpClient/4.1.1 (java 1.5)  
  27. Connection : Keep-Alive  
  28. Cookie : null  
  29. Created : 1322293843369  
  30. LastAccessed : 1322293843369  

分析: 由于这里纯粹加载页面,没有动用CookieStore自动管理Cookie,在上面没有能显示Cookie,SessionID的信息,区别于浏览器的的User-Agent,Cookie,SessionID,Accept,Accept-Language,Accept-Encoding等信息都没有进行设置.

对于爬取网站在HttpClient中设置Host,Referer,User-Agent,Connection,Cookie和爬取的频率和入口Url有讲究.

4. 考虑设置HttpClient的Header信息代码:

Java代码   收藏代码
  1. HashMap<String, String> headers = new HashMap<String, String>();  
  2. headers.put("Referer", p.url);  
  3. headers.put("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.6) Gecko/20100625   
  4.   
  5. Firefox/3.6.6 Greatwqs");  
  6. headers.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  7. headers.put("Accept-Language","zh-cn,zh;q=0.5");  
  8. headers.put("Host","www.yourdomain.com");  
  9. headers.put("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");  
  10. headers.put("Referer""http://www.yourdomian.com/xxxAction.html");  
  11. HttpRequestBase httpget = ......  
  12. httpget.setHeaders(headers);  

 5. 由新的HttpClient执行http://127.0.0.1:8080/LoginTest/index.jsp得到的HTML信息如下:

Java代码   收藏代码
  1. Protocol: HTTP/1.1  
  2. Scheme: http  
  3. Server Name: www.yourdomain.com  
  4. Server Port: 80  
  5. Protocol: HTTP/1.1  
  6. Server Info: Apache Tomcat/6.0.18  
  7. Remote Addr: 127.0.0.1  
  8. Remote Host: 127.0.0.1  
  9. Character Encoding: null  
  10. Content Length: -1  
  11. Content Type: null  
  12. Auth Type: null  
  13. HTTP Method: GET  
  14. Path Info: null  
  15. Path Trans: null  
  16. Query String: null  
  17. Remote User: null  
  18. Session Id: null  
  19. Request URI: /LoginTest/index.jsp  
  20. Servlet Path: /index.jsp  
  21. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
  22. Host: www.yourdomain.com  
  23. Referer : http://www.yourdomian.com/xxxAction.html  
  24. Accept-Language : zh-cn,zh;q=0.5  
  25. Accept-Encoding : null  
  26. User-Agent : Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6   
  27.   
  28. Greatwqs  
  29. Connection : Keep-Alive  
  30. Cookie : null  
  31. Created : 1322294148709  
  32. LastAccessed : 1322294148709  
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hgffhh/article/details/83538352