【发布时间】:2015-08-13 16:46:15
【问题描述】:
我有以下从Docusign Rest API walk through 使用的代码。由于我在代理后面,所以我添加了代理信息。
public HttpURLConnection initializeRequest(String url, String method,
String body, String httpAuthHeader) {
try {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("proxyServer address", proxyPort));
conn = (HttpURLConnection) new URL(url).openConnection(proxy);
conn.setRequestMethod(method);
conn.setRequestProperty("X-DocuSign-Authentication",
httpAuthHeader);
conn.setRequestProperty("Accept", "application/json");
if (method.equalsIgnoreCase("POST")) {
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=BOUNDARY");
conn.setDoOutput(true);
} else {
conn.setRequestProperty("Content-Type", "application/json");
}
return conn;
} catch (Exception e) {
throw new RuntimeException(e); // simple exception handling
// please review it
}
}
这运行良好,但最近代理需要身份验证,我在休息调用时收到未经授权的 401 错误。
我确实更改了代码以在其中包含身份验证器,但仍然遇到同样的问题,除此之外我可以尝试什么建议?
public HttpURLConnection initializeRequest(String url, String method, String body, String httpAuthHeader) {
try {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("proxyServerAdress", intPort));
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("username",
"password".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
conn = (HttpURLConnection) new URL(url).openConnection(proxy);
conn.setRequestMethod(method);
conn.setRequestProperty("X-DocuSign-Authentication",
httpAuthHeader);
conn.setRequestProperty("Accept", "application/json");
if (method.equalsIgnoreCase("POST")) {
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=BOUNDARY");
conn.setDoOutput(true);
} else {
conn.setRequestProperty("Content-Type", "application/json");
}
return conn;
} catch (Exception e) {
throw new RuntimeException(e); // simple exception handling
// please review it
}
}
【问题讨论】:
-
使用 requestb.in 查看您发送给 DocuSign 的内容。您使用代理的基本身份验证可能会发送到 DocuSign,这不是预期的。向您的代理人投诉。他们破坏了您的工作应用这一事实应该是他们需要解决的问题。
标签: java rest proxy docusignapi