saratbala
saratbala

Reputation: 29

IBM Watson Conversation Service Using Proxy settings from Java API

I have developed a conversation service using IBM Watson and deployed. I am able to access my service using the IBM Watson API explorer. I tried connecting the service using a Java API as explained in https://developer.ibm.com/recipes/tutorials/integration-of-ibm-watson-conversation-service-to-your-java-application/ I am working on a corporate network, so using proxy to access internet. Now I am not able to access the service from my Java API. I am getting below error.

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();

Upvotes: 0

Views: 592

Answers (2)

skvp
skvp

Reputation: 2000

After going though IBM API documentation I found below method to set the proxy. It should work.

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

I implemented this code with Java-sdk 6.14.0. IBM has discontinued ConversationService package and deprecated Conversation package in this version of SDK. Instead Assistant package has been introduced. My working code is as below.

    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();

I have checked the code with Conversation package based implementation. It worked. I couldn't checked with code given in the question as ConversationService package is no more in the current SDK.

Upvotes: 0

Varun
Varun

Reputation: 76

Not completely sure about work around for Java, but when i had similar issue with Node, i had to set up proxy variables and that helped. I would recommend you to give a try by setting up proxy variables in eclipse and JVM. And also i think this Java file must be helpful.

Upvotes: 0

Related Questions