java - URLConnection.getInputStream 返回 null

标签 java urlconnection

我使用 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 .
Web 服务在此 url 上运行:http://wafriv.de/tatoo_webservice/index.php
可以在 http://paste.geekosphere.org/p5bf 上找到发送到网络服务的 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;
}

最佳答案

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

关于java - URLConnection.getInputStream 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11939329/

相关文章:

java - 为什么我通过 JoinTable 进行的 OneToOne 映射不起作用?

java - ADF - 自定义 <af :commandButton> width the property styleClass

java - scala for 循环值不是 Int 的成员

java - 如何防止注册 Activity 重复录入

java - 不关闭 URLConnection

android - 无法解析主机 "<insert URL here>"没有与主机名关联的地址

java - 为什么 urlConnection.getContentType() 为从 url 读取的某些图像提供 null ?

java - 如何使用Java下载远程文件

java - 我在 getInputStream() 和 getOutputStream 方法上遇到错误?

java - 如何在编码/解码 java 对象时忽略字段名