기본 저지 클라이언트는 HttpURLConnection을 사용하여 요청을 서버에 보냅니다. HttpUrlConnection
이 요청에서 전송되는 일부 헤더 제한을 참조하십시오
/*
* Restrict setting of request headers through the public api
* consistent with JavaScript XMLHttpRequest2 with a few
* exceptions. Disallowed headers are silently ignored for
* backwards compatibility reasons rather than throwing a
* SecurityException. For example, some applets set the
* Host header since old JREs did not implement HTTP 1.1.
* Additionally, any header starting with Sec- is
* disallowed.
*
* The following headers are allowed for historical reasons:
*
* Accept-Charset, Accept-Encoding, Cookie, Cookie2, Date,
* Referer, TE, User-Agent, headers beginning with Proxy-.
*
* The following headers are allowed in a limited form:
*
* Connection: close
*
* See http://www.w3.org/TR/XMLHttpRequest2.
*/
private static final boolean allowRestrictedHeaders;
private static final Set<String> restrictedHeaderSet;
private static final String[] restrictedHeaders = {
/* Restricted by XMLHttpRequest2 */
//"Accept-Charset",
//"Accept-Encoding",
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Connection", /* close is allowed */
"Content-Length",
//"Cookie",
//"Cookie2",
"Content-Transfer-Encoding",
//"Date",
//"Expect",
"Host",
"Keep-Alive",
"Origin",
// "Referer",
// "TE",
"Trailer",
"Transfer-Encoding",
"Upgrade",
//"User-Agent",
"Via"
};
을 당신은이 상황을 처리하는 방법 두 가지 옵션이 있습니다
기본 저지 클라이언트를 사용하면 시스템 속성을 설정해야합니다
-Dsun.net.http.allowRestrictedHeaders=true
요청에서 제한된 헤더를 제거하지 않습니다.
ApacheHttpClient/ApacheHttpClient4에는 이러한 제한이없는 것으로 보입니다. 단순히 프로젝트에 다음과 같은 종속성 중 하나를 추가
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.15</version>
</dependency>
또는
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client4</artifactId>
<version>1.15</version>
</dependency>
다음 클라이언트를 만드는 등 :
ApacheHttpClient.create(com.sun.jersey.api.client.config.ClientConfig);
또는
ApacheHttpClient4.create(com.sun.jersey.api.client.config.ClientConfig);
나는 방법 # 1에 의해 작동이 얻을 수 있었다. 실제로 # 2가 선호되었지만 http://java.net/jira/browse/JERSEY-jsp에서 지침에 따라 "java.lang.IllegalStateException : Invalid use of SingleClientConnManager : connection still allocated"오류를 지속적으로 받았습니다. 730 – SkP