【问题标题】:Java: SOAP Message fails to Send but Works in SOAPUIJava:SOAP 消息发送失败,但在 SOAPUI 中有效
【发布时间】:2013-04-29 21:59:31
【问题描述】:

我正在创建一个 SOAP 消息,然后尝试使用以下代码发送它。

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", "");
}

finally是因为我在访问本地域的url时需要绕过代理。我知道这是一种黑客行为,并且会对其他解决方案感兴趣。然而,真正的问题与超时有关。但是,每次都会超时,如果我获取 SOAP 消息并使用 SOAPUI(相同的代理设置)发送它,我会得到一个成功的响应。

来自 Java 代码的响应...

<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>

【问题讨论】:

    标签: java soap


    【解决方案1】:

    我最终使用了这个......

    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;
    }
    

    【讨论】:

    • 那么,实际上解决方案是什么?刚刚添加了 httpS 的代理?
    • 不,我没有使用 SoapConnection,而是使用 HttpRequest 作为带有给定信息的帖子。我从来不知道如何将代理与 SoapConnection 一起使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多