java - HttpURLConnection does not read the whole response -


I use the HttpURL connection to post HTTP, but I do not always get the full response. I wanted to debug this problem, but when I worked on every line, it used to work. I thought it should be an issue of time, so I added the thread and it actually worked my code, but it is only a temporary framework. I wonder why this is happening and how to solve it here The code is: Public static InputStream doPOST throws (string input, string inputMimeType, string URL, map & strings; string, string> httpHeaders, string expectedMimeType) MalformedURLException, IOException {URL U = New URL (URL); URL connection c = u.openConnection (); InputStream Inn = Null; String media type = null; If (c instanceof HttpURLConnection) {//c.setConnectTimeout (1000000); //c.setReadTimeout (1000000); HttpURLConnection h = (HttpURLConnection) c; H.setRequestMethod ("Post"); //h.setChunkedStreamingMode(-1); SetAccept (h, expected MimeType); H.setRequestProperty ("content-type", input meme type); (For string key: httpHeaders.keySet ()) {h.setRequestProperty (key, httpHeaders.get (key)); If (logger.isDebugEnabled ()) {logger.debug ("Request property key:" +++ "/ value:" + httpHeaders.get (key)); }} H.setDoOutput (true); H.connect (); Output Stream Out = h.getOutputStream (); Out.write (input.getBytes ()); Out.close (); MediaType = h.getContentType (); Logger.debug ("------------------ Sleep ------------------ Start"); Try {Thread.Sleep (2000); } Grip (Interrupted e) e.printStackTrace (); } Logger.debug ("------------------ Sleep ------------------ End"); If (h.getResponseCode () <400) {in = h.getInputStream (); } And {in = h.getErrorStream (); }} Return; }

What to do next after reading the input current

  ByteArrayOutputStream Bose = New ByteArrayOutputStream (); While (is available ()> gt; {bos.write (is.read ()); }         is close(); //is.read(bytes); If (logger.isDebugEnabled ()) {logger.debug ("response number is:" + is.available ()); //logger.debug("RW response is "+ new string (bytes)); Logger.debug ("RAW response" + new string (Bose.Toitere ()); }  

Genearates accept the following HTTP header

  post / emailauthentication / HTTP / 1.1: application / xml Content Type: application / xml authorization: OAuth oauth_consumer_key = "B465472b-d872-42b9-030e-4e74b9b60e39", oauth_nonce = "ynDb5eepuLm% 2Fbs", oauth_signature = "Dbn% 2FWeWs2G00mk% 2BX6uIi3thJxlM% 3d", oauth_signature_method = "HMAC-SHA1", oauth_timestamp = "+1276524 919", oauth_token = "", Oauth_version = "1.0" user-agent: Java / 1.6.0_20 host: test: 6580 connection: surviving content-length: 1107  

Other suggestions were suggested in this Was

  http.keepAlive = false  

cyst Use the property to keep it alive, I tried and accept the header

  post / emailauthentication / HTTP / 1.1: application / xml Content type: application / xml authorization: OAuth oauth_consumer_key = "B465472b-d872-42b9-030e-4e74b9b60e39", oauth_nonce = "Eaiezrj6X4Ttt0", oauth_signature = "ND9fAdZMqbYPR2j% 2FXUCZmI90rSI% 3D", oauth_signatur e_method = "HMAC-SHA1", oauth_timestamp = "+1276526608", oauth_token = "", oauth_version = "1.0" User-agent: Java / 1.6.0_20 Host: Test: 6580 Connection: Closed Content-Length: 1107  

The connection header is "closed" but I still Can not read the full response Any ideas what I do wrong?

I think your problem is in this line:

  While (is available) & gt; 0] {  

According to javadoc, does not block available and does not wait for all data available, so you can get the packet first and then this false Will return. The proper way to read from the inputstream is:

  int len; Byte [] buffer = new byte [4096]; While (-1! = (Len = in.read (buffer))) {bos.write (buffer, 0, lane); }  

Read 1 will return when there is nothing left in the inputstream or the connection is closed, and by doing so will block the network and wait. Reading Arrays also performs more than single bytes.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -