我正在创建一个 httpClient,我想向我的 HttpGet 请求添加某些标头
我当前的代码产生以下请求。
GET /folder/index.html HTTP/1.0
主机:localhost:4444
连接:Keep-Alive
用户代理:Apache-HttpClient/4.2.1 (java 1.5)
我想要的是在该请求中添加另一个标头(If-Modified-Since)。
我该怎么做?
谢谢 :)
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
}