2012-08-13 52 views
2

我使用Java 6將XML數據發送到服務器。URLConnection.getInputStream返回null

XMLWriter類中的writeToURL方法使用URLConnection將XML數據發送到我的Web服務。 XML已成功傳輸並存儲在數據庫中,但不是接收下面提到的響應,而是僅收到空值。也沒有拋出異常。

作爲導致應該有下面的XML或含有它的InputStream:

<?xml version="1.0" encoding="UTF-8"?> 
<message> 
    <type>response</type> 
    <messageID>k17436fxOD3y1ywhCX48</messageID> 
    <status>successful</status> 
</message> 

<?xml version="1.0" encoding="UTF-8"?> 
<message> 
    <type>response</type> 
    <messageID>k17436fxOD3y1ywhCX48</messageID> 
    <status>alreadyExisting</status> 
</message> 

XML類的完整代碼可以發現here
這個網址的web服務運行:http://wafriv.de/tatoo_webservice/index.php
一個XML樣本發送到Web服務可以在http://paste.geekosphere.org/p5bf

任何幫助,將不勝感激被發現。


我有問題的代碼是從我的XMLWriter的writeToURL方法。
這就是:

try 
{ 
    URL url = new URL(urlString); 
    URLConnection connection = url.openConnection(); 
    connection.setUseCaches(false); 
    connection.setDoOutput(true); 
    connection.setRequestProperty("accept-charset", charset); 
    connection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
    String query = ""; 

    if (additionalParams != null) 
    { 
     for (Map.Entry<String, Object> entry : additionalParams.entrySet()) { 
      String key = entry.getKey(); 
      Object value = entry.getValue(); 
      query += String.format("%s=%s&", URLEncoder.encode(key, charset), URLEncoder.encode(value.toString(), charset)); 
     } 
    } 

    TransformerFactory transFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transFactory.newTransformer(); 
    DOMSource source = new DOMSource(this.document); 
    StringWriter sw = new StringWriter(); 
    StreamResult result = new StreamResult(sw); 
    transformer.transform(source, result); 

    query += String.format("%s=%s", URLEncoder.encode("message", charset), URLEncoder.encode(sw.toString(), charset)); 

    OutputStreamWriter writer = null; 
    InputStream response; 
    try 
    { 
     writer = new OutputStreamWriter(connection.getOutputStream(), charset); 
     writer.write(query); // Write POST query string (if any needed). 
    } 
    finally 
    { 
     if (writer != null) 
     { 
      try 
      { 
       writer.close(); 
      } 
      catch (IOException ex) 
      { 
       this.exceptionList.add(ex); 
      } 
     } 
    } 

    response = connection.getInputStream(); 

    return response; 
} 
catch (TransformerConfigurationException ex) 
{ 
    this.exceptionList.add(ex); 
} 
catch (TransformerException ex) 
{ 
    this.exceptionList.add(ex); 
} 
catch (MalformedURLException ex) 
{ 
    this.exceptionList.add(ex); 
} 
catch (IOException ex) 
{ 
    this.exceptionList.add(ex); 
} 
    catch (Exception ex) 
{ 
    this.exceptionList.add(ex); 
} 
finally 
{ 
    this.printExceptions(); 
    return null; 
} 
+1

請顯示您遇到問題的Java代碼。 – Stephan 2012-08-13 17:38:12

+0

我已經添加了代碼。它也可以在http://paste.geekosphere.org/qxwb上找到,正如我上面已經提到的那樣。 – Neithan 2012-08-13 18:29:27

+0

問題解決了。 finally最終導致writeToURL方法返回null。 – Neithan 2012-08-16 17:08:14

回答

0

的問題就解決了。 finally導致writeToURL方法返回null。