Java.io.IOException:Attempted read from closed stream

Source: Internet
Author: User

The code below is executed with the hint "java.io.IOException:Attempted read from closed stream."
@Test Public voidtest_temp () {String URL= "Http://ssov1.59iedu.com/login?" target=http://med.ihbedu.com:80/gateway/web/sso/auth&js&callback=loginthen&1470491151264& nocache=1470491171451 ";  This.    HttpGet (URL); }     Public voidhttpget (String url) {closeablehttpclient httpClient= Httpclients.createdefault ();//Establish HttpClientHttpGet HttpGet =NewHttpGet (URL);//Establish HttpGetSYSTEM.OUT.PRINTLN ("Get Request Address:" +Httpget.geturi ()); Try{closeablehttpresponse response= Httpclient.execute (HttpGet);//perform a GET request and save the resultSYSTEM.OUT.PRINTLN ("Get Request returned status code:" +response.getstatusline (). Getstatuscode ()); Httpentity httpentity= Response.getentity ();//Convert saved response to Entities            Try {                if(Httpentity! =NULL) {{System.out.println (The response value returned by the GET request: "+entityutils.tostring (httpentity)); System.out.println ("LT's Value:" +entityutils.tostring (httpentity). Split ("Lt:\") [1].split ("\", ") [0]); }                }            } finally{entityutils.consume (httpentity);//Close EntityResponse.close ();//Close Response            }        } Catch(IOException e) {e.printstacktrace (); } finally {            Try{httpclient.close ();//Close HttpClient}Catch(IOException e) {e.printstacktrace (); }        }    }
The reason for this is to specifically point out the following 2 outputs in my script, which call 2 entityutils.tostring (httpentity) in this output, and according to HttpClient's official instructions, entityutils.tostring ( Httpentity) This is called once and will be automatically destroyed, and I call 2 times all the error

SYSTEM.OUT.PRINTLN ("Response value returned by GET request:" + entityutils.tostring (httpentity));
System.out.println ("LT's Value:" +entityutils.tostring (httpentity). Split ("Lt:\" ") [1].split (" \ ",") [0]);

So the 2 output script is changed to the following, as long as the call is good

String responsestr=entityutils.tostring (httpentity);
SYSTEM.OUT.PRINTLN ("Get request Returns the response value:" + responsestr);
String str=responsestr.split ("lt:\") [1].split ("\", ") [0];
System.out.println ("LT's Value:" +STR);

Java.io.IOException:Attempted read from closed stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.