Ken*_*Ken 1 java http httpclient http-headers
我正在创建一个httpClient,我想在我的HttpGet请求中添加某些标头
我当前的代码产生以下请求.
GET /folder/index.html HTTP/1.0
主机:localhost:4444
连接:Keep-Alive
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
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在HttpGet对象上使用setHeader()方法,如下所示.
httpGet.setHeader("If-Modified-Since","11/26/2012");
Run Code Online (Sandbox Code Playgroud)
我使用这个JavaDoc作为参考.
| 归档时间: |
|
| 查看次数: |
15857 次 |
| 最近记录: |