The client an HTTP connection consists of two directions, one for this HTTP connection, and the other for the HTTP connection output.

Source: Internet
Author: User

1. The client an HTTP connection consists of two directions, one for this HTTP connection, and the other for the HTTP connection output.

After the IP address and port number are connected using httpclient, the HTTP output is set as the HTTP request parameter. The HTTP output is used for HTTP request settings.

The HTTP input is used to receive data sent back from the server.

There is a key http.openconencetion () method to initiate the connection. and Httpconn.getinputstream () are used to receive data returned by the server side.

1, the client gets the JSON string Public classhttputil{ Public Staticstring Getjsoncontent (String urlstr) {Try        {//Get httpurlconnection Connection objectURL url =NewURL (URLSTR); HttpURLConnection Httpconn=(httpurlconnection) URL. OpenConnection (); //Setting Connection PropertiesHttpconn.setconnecttimeout ( the); Httpconn.setdoinput (true); Httpconn.setrequestmethod ("GET"); //get the corresponding code            intRespcode =Httpconn.getresponsecode (); if(Respcode = = $)            {                returnConvertstream2json (Httpconn.getinputstream ()); }        }        Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return ""; }       Private Staticstring Convertstream2json (InputStream inputstream) {string Jsonstr=""; //bytearrayoutputstream equivalent to memory output streamBytearrayoutputstream out=NewBytearrayoutputstream (); byte[] buffer =New byte[1024x768]; intLen =0; //transfer the input stream to the memory output stream        Try        {             while(len = inputstream.read (buffer,0, buffer.length))! =-1)            {                 out. write (buffer,0, Len); }            //converting a memory stream to a stringJsonstr =NewString ( out. Tobytearray ()); }        Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnJsonstr; }}

The client an HTTP connection consists of two directions, one for this HTTP connection, and the other for the HTTP connection output.

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.