Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: SOAP Message fails to Send but Works in SOAPUI

Tags:

java

soap

I am creating a SOAP message and then trying to send it using the following code.

try {
  System.setProperty("http.proxyHost", "proxy.myproxy.com");
  System.setProperty("http.proxyPort", "80");
  connection = SOAPConnectionFactory.newInstance().createConnection();
  response = connection.call(message, "https://www.service.com/call");  
  connection.close();
} catch (SOAPException e) {
  e.printStackTrace();
} catch (UnsupportedOperationException e) {
  e.printStackTrace();
} 
finally{
  System.setProperty("http.proxyHost", "");
  System.setProperty("http.proxyPort", "");
}

The finally is because I need to bypass the proxy when I access urls on the local domain. I know this is kind of a hack and would be interested in other solutions. However, the real issue deals with a Timeout. This times out every time, however, if I take the SOAP message and send it using SOAPUI (same proxy settings) I get a successful response.

Response from Java Code...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>java.net.ConnectException: Connection timed out: no further information</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>
like image 393
Jackie Avatar asked Apr 29 '13 21:04

Jackie


1 Answers

I ended up using this...

public static String soapHttpRequest(RequestInfo re) {
    HttpURLConnection connection = null;
    OutputStreamWriter wr = null;
    BufferedReader rd = null;
    StringBuilder sb = null;
    String line = null;

    URL serverAddress = null;

    System.setProperty("http.proxyHost", "proxy.my.com");
    System.setProperty("http.proxyPort", "80");
    System.setProperty("https.proxyHost", "proxy.my.com");
    System.setProperty("https.proxyPort", "80");

    try {
                    serverAddress = new URL("https://site.com/service/default.asp");
        // set up out communications stuff
        connection = null;

        // Set up the initial connection
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
        connection.setRequestProperty("SOAPAction", 
              "https://site.com/service/default.asp#Method");
        OutputStream reqStream = connection.getOutputStream();
        reqStream.write(getSoapMessageString(re).getBytes());
        connection.connect();


        // read the result from the server
        rd = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        sb = new StringBuilder();

        while ((line = rd.readLine()) != null) {
            sb.append(line + '\n');
        }
        String val = sb.toString();
        System.out.println(val);
        return val;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // close the connection, set all objects to null
        connection.disconnect();
        rd = null;
        sb = null;
        wr = null;
        connection = null;
              // Closing to prevent issues with local calls
        System.setProperty("http.proxyHost", "");
        System.setProperty("http.proxyPort", "");
        System.setProperty("https.proxyHost", "");
        System.setProperty("https.proxyPort", "");
    }
    return null;
}
like image 73
Jackie Avatar answered Oct 16 '22 03:10

Jackie