2016-06-20 70 views
1

我正在嘗試使用HTTP post將文件發送到需要基本身份驗證的Web服務器。使用代理服務器的Java中的HTTP基本身份驗證

我的工作場所最近實施了對代理服務器的更改,現在它還需要基本身份驗證。

如何在單個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

您需要在以下行的HTTP標頭添加,如basic authorization描述:

  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

相關問題