我正在創建一個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