2016-06-20 40 views
1

基本認証が必要なWebサーバーにファイルを送信するためにHTTP postを使用しようとしています。プロキシを使用したJavaのHTTP基本認証

最近私の職場ではプロキシサーバーが変更されており、基本認証も必要です。

これらの両方のサーバーの資格情報を1回のHttpPost要求で入力するにはどうすればよいですか。

答えて

2

は私も "プロキシ認証" ヘッダを追加する必要が判明。

HttpPost httpPost = new HttpPost("http://host:port/test/login"); 

String encoding = Base64Encoder.encode ("your_user:your_password"); 
httpPost.setHeader("Authorization", "Basic " + encoding); 

String proxyEncoding = Base64Encoder.encode ("proxy_user:proxy_password"); 
httpPost.setHeader("Proxy-Authorization", "Basic " + proxyEncoding); 

System.out.println("executing request " + httpPost.getRequestLine()); 
HttpResponse response = httpClient.execute(httpPost); 
0

あなたはbasic authorizationで説明したように、ライン以下にHTTPヘッダに追加する必要があります。

  1. ユーザ名とパスワードは1つのコロンと組み合わされます。

  2. 結果の文字列は、RFC2045-MIMEバリアント Base64を使用してエンコードされます(ただし、76文字/行に限定されません)。

  3. 次に、エンコードされた文字列の前に、認証方法と空白、つまり「基本」を配置します。

のApache HttpClientを持つ例を見て:

String encoding = Base64Encoder.encode ("your_login:your_password"); 
HttpPost httppost = new HttpPost("http://host:post/test/login"); 
httppost.setHeader("Authorization", "Basic " + encoding); 

System.out.println("executing request " + httppost.getRequestLine()); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
+0

したがって、ログインとパスワードはWebサーバー用です。プロキシサーバーのユーザー名とパスワードはどうですか? – satbot

関連する問題