我正在创建一个httpClient,并且想要向我的HttpGet请求添加某些标头
我当前的代码产生了以下请求。
GET /folder/index.html HTTP/1.0
主机:本地主机:4444
连接:保持连接
用户代理: Apache-HttpClient/4.2.1 (java 1.5)
我想要的是在该请求中添加另一个标头(If-Modified- that )。
我该怎么做呢?
谢谢您:)
public String httpGet(String s) {
String url = s;
StringBuilder body = new StringBuilder();
httpclient = new DefaultHttpClient(); // create new httpClient
HttpGet httpGet = new HttpGet(url); // create new httpGet object
try {
response = httpclient.execute(httpGet); // execute httpGet
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// System.out.println(statusLine);
body.append(statusLine + "\n");
HttpEntity e = response.getEntity();
String entity = EntityUtils.toString(e);
body.append(entity);
} else {
body.append(statusLine + "\n");
// System.out.println(statusLine);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpGet.releaseConnection(); // stop connection
}
return body.toString(); // return the String
}
发布于 2012-11-27 02:02:30
在HttpGet对象上使用setHeader()方法,如下所示。
httpGet.setHeader("If-Modified-Since","11/26/2012");
我使用这个JavaDoc作为参考。
发布于 2014-09-05 05:39:35
在HttpGet对象上使用setHeader()方法,第一个方法如下所示
httpGet.setHeader("If-Modified-Since","11/26/2012");
然后在HttpGet对象上使用addHeader()方法,对于第二个标头如下所示。
httpGet.addHeader("If-Expires-On","11/26/2014");
https://stackoverflow.com/questions/13576294
复制