는이 같은 HTTP 요청에 대한 사용자 에이전트를 설정하려고 :Java http User-Agent를 설정할 수없는 이유는 무엇입니까?
public BufferedReader readURL(String url){
URL urlcon;
BufferedReader in = null;
try {
urlcon = new URL(url);
connection = (HttpURLConnection)urlcon.openConnection();
System.setProperty("http.agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
System.out.println(connection.getHeaderField("User-Agent"));
connection.connect();
in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String header = connection.getHeaderField(0);
System.out.println(header);
System.out.println("---Start of headers---");
int i = 1;
while ((header = connection.getHeaderField(i)) != null) {
String key = connection.getHeaderFieldKey(i);
System.out.println(((key==null) ? "" : key + ": ") + header);
i++;
}
System.out.println(connection.getHeaderField("http.agent"));
System.out.println("---End of headers---");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
그리고 내가 얻은 것은 사용자 에이전트 널 (null) :
널
HTTP/1.0 200 OK
--- 시작 헤더 ---
서버 : 아파치
캐시 제어 : 최대 사용 기간 = 10
만료 : 2011년 (일) 8월 7일을 그리니치 표준시 16시 09분 26초
비바리 : 수락 - 인코딩
을의 Content-Type : text/html과
콘텐츠 길이 : 163,582
날짜 : 2011년 (일) 8월 7일 그리니치 표준시 16시 09분 20초
X-니스 : 889,692,780 889,684,459
나이 : 4
연결 : 연결 유지
X-bip (빨리) : 889,692,780 70 148
통해 1.1 CachOS
널
--- 헤더의 끝 ---
가 왜 사용자 에이전트를 설정할 수 없습니다?
가능한 중복 [A HttpURLConnection의의 헤더를 수정하는 방법 (http://stackoverflow.com/questions/480153/how-to-modify-the-header-of-a-httpurlconnection) –
나는이 다른 주제를 따르려고 노력했지만 아무것도 작동하지 않습니다. JDK 1.6 업데이트 24를 사용합니다. – Felipe