1
0
-1

Hi,

I'm building a service to connect to our back end system via http. However, I get a type check error when I import org.apache.http.protocol.HttpContext.

I need the context to handle credentials,and then use the setContext() method of the getRequest.


Is there a work around to be able to create a context and then add it to the request?

code snippet below

CredentialsProvider cp = new BasicCredentialsProvider()
cp.setCredentials(AuthScope.ANY, new NTCredentials(username, password, null, null))

HttpContext context = HttpClientContext.create()
context.setCredentialsProvider(cp)

HttpResponse response = new GetRequest(endpoint)
.setContext(context)
.addHeaders(headers)
.setMessage(soapEnvelope)
.execute()

Thanks


    CommentAdd your comment...

    3 answers

    1.  
      1
      0
      -1

      Hi Mark

      You should be able to set those directly to GetRequest object:

      public HttpRequest setNtlmAuth(String username, String password, String workstation, String domain)
      
      
      /* @since 5.1.3  */
      public HttpRequest setProxyAuth(String username, String password)
      public HttpRequest setProxy(String proxyHost, int proxyPort)


      Please note that setContext() accepts objects of type HttpClientContext (but not HttpContext), so you have to change your code to this:

      HttpClientContext context = HttpClientContext.create()


      Cheers
      Rado

      1. Mark Murray

        Hi Ben, Radoslav,

        Thanks for the quick reply. Great work.

        Ben,

        Yes I tried the setBasicAuth; that was the first thing I tried, but received 401 Unauthorised errors.

        Radoslav,

        the method to setNtlmAuth worked to overcome the type-check errors.

        So, great progress, but more work to be done to get the service deployed and system tested.


        I appreciate your replies, very helpful.

        Thanks

      CommentAdd your comment...
    2.  
      1
      0
      -1

      Hi,

      I've managed to get the authentication working. Now I am having an issue with the message content.

      I get an error the at says:

      "MIME type may not contain reserved characters"

      However, I am setting the content-type as follows:

      headers.put("Content-Type", "text/xml; charset=utf-8")


      The content is a soapEnvelope to perform an API action on our back end system.

      I have this working in a legacy service, but here in a Fluent service I get the above errors.

      Is this related to the Fluent class HttpRequest, which provides a wrapper around the apache class?

      Any ideas would be appreciated.

      Thanks

      1. Mark Murray

        Hi,

        problem solved.

        I found that using "addHeaders(Map headers)" causes errors wiht MIME type. However if I use the method "addHeader(String header) to add each header separately I can get the service to work as expected.


        Hope this helps others.

        Regards

        Mark

      CommentAdd your comment...
    3.  
      1
      0
      -1

      Hey Mark, have you tried request.setBasicAuth(username, password) ?

        CommentAdd your comment...