IBM Watson Conversation Service使用Java API中的代理设置

时间:2018-01-04 11:29:34

标签: proxy ibm-watson

我使用IBM Watson开发了一个会话服务并进行了部署。我可以使用IBM Watson API资源管理器访问我的服务。我尝试使用Java API连接服务,如https://developer.ibm.com/recipes/tutorials/integration-of-ibm-watson-conversation-service-to-your-java-application/中所述,我正在企业网络上工作,因此使用代理访问互联网。现在我无法从我的Java API访问该服务。我收到了以下错误。

Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443
    at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:182)
    at com.chat.CustomerChat.conversationAPI(CustomerChat.java:47)
    at com.chat.CustomerChat.main(CustomerChat.java:32)
Caused by: java.net.ConnectException: Failed to connect to gateway.watsonplatform.net/169.48.66.222:443
    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:187)
    at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:170)
    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
    at okhttp3.RealCall.getResponse(RealCall.java:243)
    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
    at okhttp3.RealCall.execute(RealCall.java:57)

How do we set proxy connection in IBM watson Connection service?

My code:(Modified the user credentials and workspace id here)

        ConversationService service = new ConversationService("2017-05-26");
        service.setUsernameAndPassword("dfgdfg-578a-46b6-55hgg-ghgg4343", "ssdsd455gfg");
        MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();
        String workspaceId = "fgfdgfgg-ce7a-422b-af23-gfgf56565";
        MessageResponse response = service.message(workspaceId, newMessage).execute();

2 个答案:

答案 0 :(得分:0)

不完全确定Java的解决方法,但是当我遇到与Node类似的问题时,我必须设置代理变量,这有助于。我建议您尝试在eclipseJVM中设置代理变量。而且我认为Java file必须有所帮助。

答案 1 :(得分:0)

浏览IBM API文档后,我发现了以下方法来设置代理。应该可以。

HttpConfigOptions config = new HttpConfigOptions
                .Builder()
                .proxy(new Proxy(Proxy.Type.HTTP,
                                 new InetSocketAddress("<Proxy IP>", <Proxy Port>)))
                .build();
service.configureClient(config);

我使用Java-sdk 6.14.0实现了此代码。在此版本的SDK中,IBM已停止使用ConversationService软件包和已弃用的Conversation软件包。而是引入了助手包。我的工作代码如下。

    Assistant service = null;
    Context context = null;

    if (watsonUser.equalsIgnoreCase(APIKEY_AS_USERNAME))
    {
        IamOptions iamOptions = new IamOptions.Builder().apiKey(watsonApikey).build();
        service = new Assistant(watsonVersion, iamOptions);
    }
    else
    {
        service = new Assistant(watsonVersion, watsonUser,watsonPassword);
    }

    service.setEndPoint(watsonUrl);
    if(watsonProxy != null)
    {
        HttpConfigOptions config = new HttpConfigOptions
                .Builder()
                .proxy(new Proxy(Proxy.Type.HTTP,
                                 new InetSocketAddress(watsonProxyIP, watsonProxyPort)))
                .build();
        service.configureClient(config);
    }


    String workspaceId = watsonWorkspace_id;

    InputData input = new InputData.Builder(inputStr).build();

    MessageOptions options = new MessageOptions.Builder(workspaceId)
    .context(context)
      .input(input)
      .build();

    MessageResponse response = service.message(options).execute();
    context = response.getContext();

我已经使用基于会话包的实现检查了代码。有效。我无法检查问题中给出的代码,因为当前SDK中已不再包含ConversationService软件包。