2012-11-06 7 views
5

Jerseyクライアントは私の "origin"ヘッダーを設定していません。Jersey Clientで "Origin"と "Access-Control-Request-Method"ヘッダーを設定する

String origin="http://www.localhost.com"; 
ClientResponse response= webResourceBuilder("my/endpoint") 
      .header("origin" , origin) 
      .header("Access-Control-Request-Method", "POST") 
      .header("xorigin", origin) 
      .header("whatever", "test") 
      .accept("application/xml") 
      .get(ClientResponse.class); 

私は、実行時にサーバ側のリクエストヘッダを検査するとき、私は「xorigin」と「何」のヘッダーではなく、「起源」と「アクセス・コントロール・リクエスト・メソッド」

見つけますこれらのヘッダーはどのように設定できますか?

答えて

12

デフォルトJerseyクライアントは、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" 
}; 

あなたはこの状況に対処するためにどのように2つのオプションがあります。あなたは、システムのプロパティを設定する必要があり、デフォルトジャージーのクライアントとの

  1. -Dsun.net.http.allowRestrictedHeaders=true 
    

    制限付きヘッダーを要求から削除することを抑制します。

  2. ApacheHttpClient/ApacheHttpClient4この制限がないようです。単にあなたのプロジェクトに、次の依存関係の1つを追加します。

    <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); 
    
+0

私は、これはアプローチ#1で仕事を得ることができました。 実際に#2が優先されましたが、「java.lang.IllegalStateException:Invalid use of SingleClientConnManager:connection still allocated」というエラーが表示されました。http://java.net/jira/browse/JERSEY- 730 – SkP

7

または(あなたがグローバル設定として、それを設定したくない場合は)ちょうどあなたのヘッダーを設定する前に、動的にこのプロパティを設定:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); 
+0

これは私のために働いています - 要求を行うためにHttpURLConnectionを使用するCxf Webクライアント(3.1.1)で同じ問題が発生しました。 –

関連する問題