SOAP UI HTTP Request Vs Apache CXF

I have a strange situation going on while implementing Web service client. Request fired from SOAP UI is success and the HTTP request is as below:

 POST http://xxxxxxxxxx/xx/xx/xxxxx/xxxxx HTTP/1.1
 Accept-Encoding: gzip,deflate
 Content-Type: text/xml;charset=UTF-8
 SOAPAction: "CreateUserSoap"
 Content-Length: 1490
 Host: xxxxxxxxxxxxxxxxxxx.net
 Connection: Keep-Alive
 User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
 Cookie: SMCHALLENGE=YES
 Cookie2: $Version=1
 Authorization: Basic ZHFhbgfdd6RFFQcdfgdm9vccQ=
 <soap........./>

However when i fire the web service request from the application, for which i use apace cxf following HHTP Request is generated and i get a 403:Forbidden Error

ID: 1
Address: http://xxxxxxxxxx/xx/xx/xxxxx/xxxxx
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], Authorization=[Basic ZHFhbgfdd6RFFQcdfgdm9vccQ=], SOAPAction=    ["CreateUserSoap"]}
Payload: <soap....... />

I am not sure if the apache – cxf is generating the right http header as expected by the server. the soap envelope is same in both cases.

Below is the implementing java client code:

    ClientWs clientWS= new ClientWs ();
    ClientWSPortType portType = clientWS.getClientWSPort();
    BindingProvider provider = (BindingProvider) portType;

    Header dummyHeader = new Header(new QName("http://clientWS/wsdl", "ClientWS"), documentBuilder.parse(new ByteArrayInputStream(xmlString2.getBytes("UTF-8"))).getDocumentElement() ); --parsing the soap mesage
    headers.add(dummyHeader);
    provider.getRequestContext().put(Header.HEADER_LIST, headers);

    provider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, user_name);
    provider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, pass_word);

Answer

Well, no one ansered neither was i able to do this with Apache CXF. The turnaround was to use core java HttpURLConnection to achieve this.

A method something like this helped the cause ad post a webservice call without any framework.:

public HttpURLConnection getHttpConn(String webservice_url) throws IOException {
URL endpoint = new URL(webservice_url);
URLConnection connection = endpoint.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] encodedBytes = Base64.encodeBase64((getUsername()+":"+getPassword()).getBytes());
httpConn.setRequestMethod(getRequestMethod());
httpConn.setRequestProperty(HTTP_ACCEPT_ENCODING, getAccept_Encoding());
httpConn.setRequestProperty(HTTP_CONTENT_TYPE, getContentType());
httpConn.setRequestProperty(getContent_Length(), getContent_Length());
httpConn.setRequestProperty(HTTP_HOST, getHost());
httpConn.setRequestProperty(getConnection(), getConnection());
httpConn.setRequestProperty(HTTP_COOKIE2, getCookie2());
httpConn.setRequestProperty(HTTP_COOKIE, getCookie());
httpConn.setRequestProperty("Authorization", "Basic "+new String(encodedBytes));
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
return httpConn;
}

Leave a Reply

Your email address will not be published. Required fields are marked *