httpClient를 만들고 HttpGet 요청에 특정 헤더를 추가하려고합니다.
현재 코드에서 다음 요청을 생성합니다.HttpGet 헤더 추가
GET /folder/index.html HTTP/1.0
호스트 : 로컬 호스트 : 4444
연결 : 연결 유지
사용자 에이전트 : 아파치 HttpClient를/4.2.1 (자바 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