0

I am trying to invoke a REST webservice using java code.

I am able to execute it thru Rest Client Postman.

My code:

    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();

I am getting below error:

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)

Can someone help?

amit
  • 181
  • 2
  • 4
  • 20
  • Tried adding proxy: System.setProperty("https.proxyHost", "we1proxy01"); System.setProperty("https.proxyPort", "8080"); Without proxy it throws error: connection refused. – amit Sep 11 '14 at 15:05
  • I have gone thru relevant other threads, they are saying this issue is from server side. But then how its executing thru rest client? Are they adding something to request body internally? – amit Sep 11 '14 at 15:09

1 Answers1

0

Since the response from server is ok, then no issues on the server side. Compare the request body send through Rest Client with the one send from code. Then try to map.

Check this tutorial link:http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

This helped me when I tried to do the same thing as you are currently trying

pd30
  • 240
  • 1
  • 7
  • Thanks. I tried to map code with rest client, but can't find any difference. I am now exploring your link. – amit Sep 12 '14 at 09:15
  • Tried the link. I am doing it the same way. No luck :( – amit Sep 12 '14 at 12:27
  • Just check if you are opening and closing the stream properly.Like if u are trying to close a already closed stream etc. – pd30 Sep 13 '14 at 18:58
  • I checked everything. Its a pretty simple code. Stream closing is proper. Still no luck :( – amit Sep 17 '14 at 14:02
  • try example from this link:http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – pd30 Sep 17 '14 at 15:11
  • A suggestion, test your code on some other machine. My experience says there is a chance that it might work on other machine and not on current one ,possibly due to environment issues. – pd30 Sep 18 '14 at 13:35
  • I have tested my code on different machines and networks. But still the issue persists. – amit Oct 09 '14 at 09:14
  • No to and fro communication detected on network by fiddler !! – amit Oct 09 '14 at 09:16