Java Webservice调用返回错误500

Java Webservice调用返回错误500,java,webservice-client,httpurlconnection,Java,Webservice Client,Httpurlconnection,我用Java启动了一个小项目。 我必须创建一个客户端,将xml作为HTTP POST请求发送到url。 我使用java.net.*package(下面是一段代码)进行了尝试,但得到的错误如下: java.io.IOException: Server returned HTTP response code: 500 for URL: "target url" at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Http

我用Java启动了一个小项目。
我必须创建一个客户端,将xml作为HTTP POST请求发送到url。
我使用
java.net.*
package(下面是一段代码)进行了尝试,但得到的错误如下:

java.io.IOException: Server returned HTTP response code: 500 for URL: "target url"
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at newExample.main(newExample.java:36)
try {
        URL url = new URL("target url");

        URLConnection connection = url.openConnection();

        if( connection instanceof HttpURLConnection )
            ((HttpURLConnection)connection).setRequestMethod("POST");

        connection.setRequestProperty("Content-Length", Integer.toString(requestXml.length()) );
        connection.setRequestProperty("Content-Type","text/xml; charset:ISO-8859-1;");
        connection.setDoOutput(true);
        connection.connect();           

        // Create a writer to the url
        PrintWriter writer = new PrintWriter(new
        OutputStreamWriter(connection.getOutputStream()));

        // Get a reader from the url
        BufferedReader reader = new BufferedReader(new
        InputStreamReader(connection.getInputStream()));

        writer.println();
        writer.println(requestXml);
        writer.println();
        writer.flush();

        String line = reader.readLine();
            while( line != null ) {
                    System.out.println( line );
                    line = reader.readLine();
            }


    } catch (MalformedURLException e) {
                    e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
我的代码如下:

java.io.IOException: Server returned HTTP response code: 500 for URL: "target url"
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at newExample.main(newExample.java:36)
try {
        URL url = new URL("target url");

        URLConnection connection = url.openConnection();

        if( connection instanceof HttpURLConnection )
            ((HttpURLConnection)connection).setRequestMethod("POST");

        connection.setRequestProperty("Content-Length", Integer.toString(requestXml.length()) );
        connection.setRequestProperty("Content-Type","text/xml; charset:ISO-8859-1;");
        connection.setDoOutput(true);
        connection.connect();           

        // Create a writer to the url
        PrintWriter writer = new PrintWriter(new
        OutputStreamWriter(connection.getOutputStream()));

        // Get a reader from the url
        BufferedReader reader = new BufferedReader(new
        InputStreamReader(connection.getInputStream()));

        writer.println();
        writer.println(requestXml);
        writer.println();
        writer.flush();

        String line = reader.readLine();
            while( line != null ) {
                    System.out.println( line );
                    line = reader.readLine();
            }


    } catch (MalformedURLException e) {
                    e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
请用合适的例子或任何其他方法来帮助

上述代码中的点错误/错误或其他可能性

我的Web服务在spring框架中


要发送的xml为字符串格式:requestXml

问题出在服务器代码或服务器配置中:

10.5.1500内部服务器错误

服务器遇到意外情况,无法满足请求

()


如果服务器在您的控制之下(如果我在编辑之前查看URL,那么应该是),那么请查看服务器日志

嗯,你应该关闭你的流和连接。自动资源管理来自Java7或可以帮助。然而,这可能不是主要问题


主要问题是服务器端出现故障。HTTP代码500表示服务器端错误。我不能告诉你原因,因为我不知道服务器端部分。也许您应该查看服务器的日志。

我认为您的问题在于,您在写入和关闭输出流之前打开了输入流。当然,政府是这样做的

如果过早打开输入流,则输出流可能会自动关闭,从而导致服务器看到一个空的POST请求。这可能足以使它感到困惑并发送500个响应

即使这不是导致500个错误的原因,也最好按照教程中列出的顺序执行操作。首先,如果在完成请求编写之前意外读取了响应,则可能(至少暂时)锁定连接。(事实上,看起来您的代码正在这样做,因为您没有在从读卡器读取之前关闭编写器。)


另一个问题是,您的代码在所有情况下都不会关闭连接,因此可能会泄漏网络连接。如果重复这样做,可能会导致更多IOException。

问题在于下面的代码

// Get a reader from the url
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
由于服务可能并不总是返回正确的响应。。。当您通过http调用服务时,可能是服务器本身不可用或服务不可用。因此,在从流中读取响应之前,您应该始终检查响应代码,根据您必须决定是从inputStream中读取响应代码以获得成功响应,还是从errorStream中读取响应代码以获得失败或异常情况

BufferedReader reader = null;

if(connection.getResponseCode() == 200)
{
reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
}
else
{
reader = new BufferedReader(new
InputStreamReader(connection.getErrorStream()));
}

这将解决问题

如果调用外部Web服务并在REST调用中传递JSON,请检查传递值的数据类型。

例如:

{ "originalReference":"8535064088443985",
  "modificationAmount":
    { "amount":"16.0",
      "currency":"AUD"
    },
  "reference":"20170928113425183949",
  "merchantAccount":"MOM1"
}
在本例中,amount的值作为字符串发送,webservice调用失败,服务器返回HTTP响应代码:500。
但是当发送了amount:16.0,即传递了一个整数时,调用就成功了。尽管您在调用此类外部API时参考了API文档,但类似这样的小细节可能会丢失。

HTTP错误500是“内部服务器错误”,是服务遇到错误或引发异常时返回的一般错误。您可能需要阅读完整的响应正文,并查看是否有其他信息错误代码500是一个内部服务器错误。所以我会把注意力放在服务器上,而不是客户机上。如果你的URL是正确的(可以从浏览器中查看),你就无能为力了。看起来像服务器端错误。getResponeCode,此方法实际存在吗?我在这里找不到它-我将连接对象键入HttpURLConnection,然后我可以得到响应代码。