HttpUrlConnection getOutputStream()抛出java.net.SocketException:来自服务器的文件意外结束

时间:2014-09-11 15:04:22

标签: java web-services

我正在尝试使用java代码调用REST Web服务。

  

我可以通过Rest Client Postman执行它。

我的代码:

    URL url = new URL(appURL);
    httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setRequestMethod("POST");
    httpCon.setDoInput(true);
    httpCon.setUseCaches(false);
    httpCon.setAllowUserInteraction(false);
    httpCon.setDoOutput(true);
    httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpCon.setRequestProperty("Host", "www.astecanalytics.com");
    httpCon.setRequestProperty("Expect", "100-continue");

    OutputStream outstream = httpCon.getOutputStream();

我收到以下错误:

java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:772)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1789)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
    at com.ktt.business.keywords.webservice.ExecuteWebServiceKeyword.postHTTPRequestAndGetRespone(ExecuteWebServiceKeyword.java:618)

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

由于服务器的响应正常,因此服务器端没有问题。 将通过Rest Client发送的请求主体与从代码发送的请求主体进行比较。 然后尝试映射。

查看本教程链接:http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

当我尝试做与你正在尝试相同的事情时,这对我有帮助