我正在尝试使用HTTP post将文件发送到需要基本身份验证的Web服务器。
我的工作场所最近实施了对代理服务器的更改,现在还需要进行基本身份验证。
如何在单个HttpPost请求中输入这两个服务器的凭据?
答案 0 :(得分: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);
答案 1 :(得分:0)
用户名和密码与单个冒号组合在一起。
使用RFC2045-MIME变体对结果字符串进行编码 Base64,但不限于76 char / line。
查看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();