【问题标题】:URLConnection.getInputStream returns nullURLConnection.getInputStream 返回 null
【发布时间】:2012-08-13 17:32:48
【问题描述】:

我使用 Java 6 将 XML 数据发送到服务器。

XMLWriter 类中的 writeToURL 方法使用 URLConnection 将 XML 数据发送到我的 Web 服务。 XML 已成功传输并存储在数据库中,但没有收到下面提到的响应,我只得到 null。也没有抛出异常。

因此应该有以下 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找到。
网络服务在这个 url 上运行:http://wafriv.de/tatoo_webservice/index.php
可以在http://paste.geekosphere.org/p5bf上找到要发送到 Web 服务的 XML 示例

任何帮助将不胜感激。

[编辑]
我遇到问题的代码是我的 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;
}

【问题讨论】:

  • 请显示您遇到问题的java代码。
  • 我已经添加了代码。正如我上面已经提到的,它也可以在paste.geekosphere.org/qxwb 上找到。
  • 问题解决了。 finally 导致 writeToURL 方法返回 null。

标签: java urlconnection


【解决方案1】:

问题解决了。 finally 导致 writeToURL 方法返回 null。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2020-06-19
    • 2011-10-15
    相关资源
    最近更新 更多