我已经用Java编写了一个代码段,该代码段连接到IBM Watson Assistant API。
该代码在我的家庭网络中运行良好,但是在尝试在公司网络中运行时却出现以下错误。我在调用API之前已设置代理:
System.getProperties().put("https.proxyHost", ip);
System.getProperties().put("https.proxyPort", port);
System.getProperties().put("https.proxyUser", user);
System.getProperties().put("https.proxyPassword", pwd);
当我打印端点时,得到以下数据:
URL-->https://gateway.watsonplatform.net/assistant/api
错误:
java.lang.RuntimeException: java.io.IOException: Failed to authenticate with proxy
at com.ibm.watson.developer_cloud.service.WatsonService$WatsonServiceCall.execute(WatsonService.java:478)
at com.fca.pd.chatbot.WatsonConversationApi.watsonService(WatsonConversationApi.java:162)
at com.fca.pd.chatbot.WatsonConversationApi.doPost(WatsonConversationApi.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
最佳答案
您需要使用OkHttp
客户端配置
这是使用助手服务的完整示例:
Assistant service = new Assistant("2018-02-16") {
@Override
protected OkHttpClient configureHttpClient() {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", "proxyPort"));
return super.configureHttpClient().newBuilder().proxy(proxy).build();
}
};
service.setUsernameAndPassword("<username>", "<password>");
WorkspaceCollection workspaces = service.listWorkspaces().execute();
System.out.println(workspaces);
确保替换
"proxyHost"
和"proxyPort"
有关如何设置代理的更多信息,请查看OkHTTPClient Proxy authentication how to?