0

I'm getting a problem when trying to read the response from a web service after using a POST request. I'm expecting to get some useful XML back, but instead all I am getting is HTML.

    URL url = new URL("https://abc.co.uk/someWS");
    String pKeyPassword = "xxxxxx";
    String xmlOutput = "someXML...";

    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

    //Load authentication certificate.
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    KeyStore keyStore = KeyStore.getInstance("PKCS12");

    InputStream keyInput = new FileInputStream("/home/keystore.p12");
    keyStore.load(keyInput, pKeyPassword.toCharArray());
    keyInput.close();

    keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

    con.setSSLSocketFactory(context.getSocketFactory());

    // Tell the connection that we will be sending information
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Content-Length", "" + xmlOutput.length());
    con.setRequestProperty("Content-Type", "text/xml; UTF-8");
    con.setRequestMethod("POST");

    con.connect();

    // Send the POST stream data
    DataOutputStream outputStream = new DataOutputStream(con.getOutputStream());
    outputStream.writeBytes(xmlOutput);

    // Read the response
    InputStream inputstream = con.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

    // format response to a string
    String string = null;
    String response = "";
    while ((string = bufferedreader.readLine()) != null) {
      response += string;
    }
    con.disconnect();
    System.out.println(response);

I've been assured that there is nothing wrong with the web service I am connecting to, and apparently it looks like from their end I'm trying to do a GET request (which will return HTML) rather than a POST request. Any idea whats wrong here?

antz
  • 63
  • 2
  • 5
  • Please, add some info about HTML you got – Maxim Shoustin Nov 02 '12 at 22:49
  • Its just a table with a list of all the web services available from this server, which has a URL which points to the same page I'm connecting too. It returns a 200 OK response. – antz Nov 02 '12 at 23:01
  • Take a look on my response. Install soapUI and load all web services (just put WSDL path) there. After , you can play with each WS and verify that *clear* webs ervice you defined before works. – Maxim Shoustin Nov 02 '12 at 23:03
  • I installed soapUI and connected to the web service and sent my XML... and it worked exactly how I would expect with the correct XML being returned...?! – antz Nov 02 '12 at 23:39
  • So problem with your code. Try to use `org.apache.axis.message.SOAPEnvelope`. I can give you example if you want – Maxim Shoustin Nov 02 '12 at 23:47
  • I added some code to my previous response – Maxim Shoustin Nov 02 '12 at 23:57

2 Answers2

0

Suppose if you got HTML, it might be some server error. Please, read server logs to detect that issue. Anyways, you can use soapUI application to verify that your Web Service defined and work properly.

Here is snippets of code that worked for me (just add password):

import org.apache.axis.AxisFault;
import org.apache.axis.SOAPPart;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.soap.MessageFactoryImpl;
....
String mHostAddr = "yourURL";
try {

        String envelope = getXML(); //redFromXmlFile();
        //String envelope = "";
        byte[] reqBytes = envelope.getBytes();
        ByteArrayInputStream bis = new ByteArrayInputStream(reqBytes);
        StreamSource ss = new StreamSource(bis);

        //Create a SOAP Message Object
        MessageFactoryImpl messageFactory = new MessageFactoryImpl();
        SOAPMessage msg = messageFactory.createMessage();
        SOAPPart soapPart = (SOAPPart) msg.getSOAPPart();

        //Set the soapPart Content with the stream source
        soapPart.setContent(ss);

        //Create a WebService Call
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTargetEndpointAddress(mHostAddr);

        //Invoke the WebService.
        SOAPEnvelope resp = call.invoke(((org.apache.axis.SOAPPart)soapPart).getAsSOAPEnvelope());






    } catch (AxisFault ex) {
        System.out.println(ex.getMessage());
    } catch (ServiceException ex) {
        System.out.println(ex.getMessage());
    } catch (SOAPException ex) {
        System.out.println(ex.getMessage());
    } catch (RemoteException e) {
        e.printStackTrace();
    } 


....

It might help you or find right direction

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
0

You seem to be calling your webservice "the hard way" (TM).

There are numerous frameworks (Metro, CXF, etc.) that can generate stub classes for you that you can call quite easily and you are far less likely to have to deal with your current issue. wsimport is your friend and will tell you if the WSDL in question isn't correct.

See this answer for some relevant links.

Community
  • 1
  • 1
Catchwa
  • 5,845
  • 4
  • 31
  • 57