用Firebug对POST的数据进行监控 请求 HTTP头 信息,得到如下内容:
Accept application/json, text/javascript, */* Accept-Encoding gzip, deflate Accept-Language en-us,en;q=0.5 Cache-Control no-cache Content-Length 432 Content-Type application/x-www-form-urlencoded; charset=UTF-8 Host www.huaxixiang.com Pragma no-cache Proxy-Connection keep-alive Refere http://www.huaxixiang.com/CrossPriceDetail.action User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 X-Requested-With XMLHttpRequest
用HttpClient模仿浏览器访问页面,加载URL的HTML信息,为了良好的加载网站的信息,不被限制.
为了说明请求头的信息添加了一个小测试项目LoginTest,添加页面index.jsp,添加主要代码打印Http Header的JSP页面.
主要打印Http Header信息.
1. index.jsp
out.println("Protocol: " + request.getProtocol()); out.println("Scheme: " + request.getScheme()); out.println("Server Name: " + request.getServerName() ); out.println("Server Port: " + request.getServerPort()); out.println("Protocol: " + request.getProtocol()); out.println("Server Info: " + getServletConfig().getServletContext().getServerInfo()); out.println("Remote Addr: " + request.getRemoteAddr()); out.println("Remote Host: " + request.getRemoteHost()); out.println("Character Encoding: " + request.getCharacterEncoding()); out.println("Content Length: " + request.getContentLength()); out.println("Content Type: "+ request.getContentType()); out.println("Auth Type: " + request.getAuthType()); out.println("HTTP Method: " + request.getMethod()); out.println("Path Info: " + request.getPathInfo()); out.println("Path Trans: " + request.getPathTranslated()); out.println("Query String: " + request.getQueryString()); out.println("Remote User: " + request.getRemoteUser()); out.println("Session Id: " + request.getRequestedSessionId()); out.println("Request URI: " + request.getRequestURI()); out.println("Servlet Path: " + request.getServletPath()); out.println("Accept: " + request.getHeader("Accept")); out.println("Host: " + request.getHeader("Host")); out.println("Referer : " + request.getHeader("Referer")); out.println("Accept-Language : " + request.getHeader("Accept-Language")); out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding")); out.println("User-Agent : " + request.getHeader("User-Agent")); out.println("Connection : " + request.getHeader("Connection")); out.println("Cookie : " + request.getHeader("Cookie")); out.println("Created : " + session.getCreationTime()); out.println("LastAccessed : " + session.getLastAccessedTime());
2. 使用IE浏览器加载http://127.0.0.1:8080/LoginTest/index.jsp返回内容如下:
Protocol: HTTP/1.1 Scheme: http Server Name: 127.0.0.1 Server Port: 8080 Protocol: HTTP/1.1 Server Info: Apache Tomcat/6.0.18 Remote Addr: 127.0.0.1 Remote Host: 127.0.0.1 Character Encoding: null Content Length: -1 Content Type: null Auth Type: null HTTP Method: GET Path Info: null Path Trans: null Query String: null Remote User: null Session Id: E2C384C095E34AD355684EB554517FB1 Request URI: /LoginTest/index.jsp Servlet Path: /index.jsp Accept: */* Host: 127.0.0.1:8080 Referer : null Accept-Language : en-us Accept-Encoding : gzip, deflate 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) Connection : Keep-Alive Cookie : JSESSIONID=E2C384C095E34AD355684EB554517FB1 Created : 1322294859981 LastAccessed : 1322294859981
3. 后面使用HttpClient不设置header信息加载http://127.0.0.1:8080/LoginTest/index.jsp信息如下:
Protocol: HTTP/1.1 Scheme: httpServer Name: 127.0.0.1 Server Port: 8080 Protocol: HTTP/1.1 Server Info: Apache Tomcat/6.0.18 Remote Addr: 127.0.0.1 Remote Host: 127.0.0.1 Character Encoding: null Content Length: -1 Content Type: null Auth Type: null HTTP Method: GET Path Info: null Path Trans: null Query String: null Remote User: null Session Id: null Request URI: /LoginTest/index.jspServlet Path: /index.jsp Accept: null Host: 127.0.0.1:8080 Referer : null Accept-Language : null Accept-Encoding : null User-Agent : Apache-HttpClient/4.1.1 (java 1.5) Connection : Keep-Alive Cookie : null Created : 1322293843369 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信息代码:
HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Referer", p.url); headers.put("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 Greatwqs"); headers.put("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); headers.put("Accept-Language","zh-cn,zh;q=0.5"); headers.put("Host","www.yourdomain.com"); headers.put("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"); headers.put("Referer", "http://www.yourdomian.com/xxxAction.html"); HttpRequestBase httpget = ...... httpget.setHeaders(headers);
5. 由新的HttpClient执行http://127.0.0.1:8080/LoginTest/index.jsp得到的HTML信息如下:
Protocol: HTTP/1.1 Scheme: http Server Name: www.yourdomain.com Server Port: 80 Protocol: HTTP/1.1 Server Info: Apache Tomcat/6.0.18 Remote Addr: 127.0.0.1 Remote Host: 127.0.0.1 Character Encoding: null Content Length: -1 Content Type: null Auth Type: null HTTP Method: GET Path Info: null Path Trans: null Query String: null Remote User: null Session Id: null Request URI: /LoginTest/index.jsp Servlet Path: /index.jsp Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Host: www.yourdomain.com Referer : http://www.yourdomian.com/xxxAction.html Accept-Language : zh-cn,zh;q=0.5 Accept-Encoding : null User-Agent : Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 Greatwqs Connection : Keep-Alive Cookie : null Created : 1322294148709 LastAccessed : 1322294148709
相关推荐
httpclient的用法,发送get请求和post请求,设置header
4. 添加请求头:例如设置Content-Type为JSON,`httpPost.setHeader("Accept", "application/json");` 5. 执行请求:通过HttpClient的`execute`方法发送请求,并获取响应,`CloseableHttpResponse response = ...
HTTP请求由以下部分组成:请求行、请求头、空行和请求体。常见的请求方法有GET和POST。 - **GET请求**:是最简单的HTTP请求方式,用于从服务器获取资源。它将参数附加在URL后面,不安全且有限制(通常2KB左右),...
public static String post(String url, String params){ log.info("post url:" + url + " params:" + ... httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity(stringEntity); Closeable
HttpClient提供了丰富的API来定制请求头、超时设置等。例如,你可以设置连接超时和读取超时: ```java RequestConfig config = RequestConfig.custom() .setConnectTimeout(5000) .setSocketTimeout(5000) ....
对于POST请求,我们可能需要设置请求体,这可以通过`setEntity`方法实现,如使用`StringEntity`: ```java httpPost.setEntity(new StringEntity("请求数据")); ``` 接下来,我们可以使用HttpClient发送请求并获取...
在IT行业中,网络通信是应用程序...通过创建HttpClient对象、设置请求方法、构造HTTP实体、添加请求头以及处理响应,我们可以实现与服务器的高效交互。在实际开发中,理解这些基本操作对于构建可靠的网络应用至关重要。
在Java编程中,HttpClient提供了一个强大的API,允许我们创建自定义的HTTP请求,设置请求头,携带参数,处理响应等。以下是一些使用HttpClient的关键知识点: 1. **HttpClient对象创建**:首先,我们需要创建一个...
在上述代码中,我们使用了`HttpClients.createDefault()`创建默认的HttpClient实例,然后创建`HttpGet`或`HttpPost`对象,设置URL和请求头。对于POST请求,还需设置请求体。执行请求后,我们从响应中获取实体,并将...
HttpClientUtil 是一个用于发送 HTTP 请求的工具类,主要支持 GET 和 POST 方法。它使用了 Apache HttpClient 库,这是一个强大的 Java 客户端编程工具包,用于处理 HTTP 协议。以下是对类中关键方法和概念的详细...
- 设置请求头中的`Content-Type`为`"application/json;charset=utf-8"`,表明请求体将使用JSON格式的数据。 - 设置`Accept`头为`"application/json"`,表明期望接收JSON格式的响应。 4. **设置请求体**: - 构造...
在Java编程中,HttpClient是一个广泛使用的HTTP客户端库,用于发送HTTP请求并接收响应。当遇到“status 400”错误时,这通常意味着服务器无法理解客户端发送的请求,因为请求有语法错误或者包含了无效的数据。在这个...
5. **设置请求头和重试策略**:在发送请求时,我们可以通过HttpRequestBase对象设置自定义的请求头,以期望服务器返回正确的Response Header。此外,如果服务器返回错误的Header导致请求失败,可以设定重试策略,...
`httpclient`是一个Java库,它允许我们方便地执行HTTP请求,而`fastjson`则是阿里巴巴提供的一款高性能的JSON库,用于JSON的序列化和反序列化。现在,我们将深入探讨如何使用`httpclient`进行跨域请求,并结合`...
在探讨HttpClient 4.0的`post()`方法中四个关键“头信息”的设置之前,我们首先需要理解HttpClient是什么以及它在Java开发中的作用。 ### HttpClient简介 HttpClient是Apache的一个开源项目,它为Java应用程序提供...
4. **设置请求头**: 如果服务器需要特定的头部信息,例如Content-Type,需要在此步骤中添加。 ```java httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/...
使用HttpClient调用URL的基本步骤包括创建HttpClient实例、构建HttpGet或HttpPost对象、设置请求头(如Content-Type、Authorization等)以及执行请求。 1. **HttpClient实例化**:初始化HttpClient对象,例如: ``...
2. **commons-codec**:提供了各种编码算法,如Base64、URL编码和ASCII85编码,HttpClient用它来处理请求和响应中的编码问题,特别是在处理URL、表单数据和HTTP头时。 在使用HttpClient时,我们首先需要导入相关...