【问题标题】:IBM Watson Conversation Service Using Proxy settings from Java APIIBM Watson Conversation Service 使用 Java API 中的代理设置
【发布时间】:2019-05-07 17:48:23
【问题描述】:

我使用 IBM Watson 开发了一个对话服务并进行了部署。我可以使用 IBM Watson API Explorer 访问我的服务。我尝试使用 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();

【问题讨论】:

    标签: proxy ibm-watson


    【解决方案1】:

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

    【讨论】:

    • 我在 Eclipse 和 JVM 中都试过,但没有用。得到同样的例外。我能够成功访问公司网络之外的 API。
    【解决方案2】:

    查看 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 实现了这段代码。 IBM 已停止使用此版本 SDK 中的 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();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      相关资源
      最近更新 更多