Java 中使用代理的 HTTP 基本身份验证
HTTP Basic Authentication in Java with Proxy
我正在尝试使用 HTTP post 将文件发送到需要基本身份验证的 Web 服务器。
我的工作场所最近对代理服务器进行了更改,现在也需要基本身份验证。
如何在单个 HttpPost 请求中输入这两个服务器的凭据?
您需要在您的 http header 中添加以下行,如 basic authorization 中所述:
用户名和密码由一个冒号组合而成。
生成的字符串使用 RFC2045-MIME 变体编码
Base64,除了不限于76char/line.
- 然后将授权方法和 space 即 "Basic " 放在编码字符串之前。
查看 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();
原来我还需要添加一个 "Proxy-Authorization" header。
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);
我正在尝试使用 HTTP post 将文件发送到需要基本身份验证的 Web 服务器。
我的工作场所最近对代理服务器进行了更改,现在也需要基本身份验证。
如何在单个 HttpPost 请求中输入这两个服务器的凭据?
您需要在您的 http header 中添加以下行,如 basic authorization 中所述:
用户名和密码由一个冒号组合而成。
生成的字符串使用 RFC2045-MIME 变体编码 Base64,除了不限于76char/line.
- 然后将授权方法和 space 即 "Basic " 放在编码字符串之前。
查看 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();
原来我还需要添加一个 "Proxy-Authorization" header。
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);