我正在创建一个httpClient,我想添加某些标头到我的HttpGet请求
我的当前代码产生以下请求。HttpGet添加标头
GET /folder/index.html HTTP/1.0
主机:本地主机:4444
连接:保持活动
的User-Agent: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
}
我们可以在同一个httpGet对象上添加多个setHeaders吗?我想添加多个标题。 – Darpan