<span id="Label3"></p><p>Introduction to the HTTP Protocol:<br>HTTP (hypertext Transfer Protocol), which is the foundation of Web networking and one of the most commonly used protocols for mobile networking, is a protocol built on the TCP Protocol.<br>The most notable feature of an HTTP connection is that each request sent by the client requires a server loopback response, and the connection is actively released after the request has Ended. The process from establishing a connection to closing a connection is called a "one-time connection." In HTTP 1.0, each request from the client requires a separate connection to be established, and the connection is automatically freed after the request is Processed. In HTTP 1.1, multiple requests can be processed in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next Request.<br>Because HTTP is actively releasing the connection after each request ends, the HTTP connection is a "short connection", "stateless", and to keep the client program online, a connection request needs to be made to the server continuously. The usual practice is that even if you do not need to obtain any data, the client also keeps sending a "keep-connected" request to the server at regular intervals, and the server responds to the client after receiving the request, indicating that the client is "online". If the server can not receive the Client's request for a long time, it is considered that the client "offline", if the client cannot receive a reply from the server for a long time, it is considered that the network has been disconnected.<br>The HTTP1.0 protocol-based client returns a response message to the client each time a request is made to the server, and the server shuts down the network connection after confirming that the client has received a response message. In this data transfer process, no historical information and status information is saved, so the HTTP protocol is also considered a stateless Protocol.<br>The biggest difference between HTTP1.1 and HTTP1.0 is the increased support for persistent connections. When the client connects to the server using the HTTP1.1 protocol, the server returns the initiative of shutting down the client connection to the client, which means that the HTTP request can continue to be sent to the server, as long as the close method of the socket class is not called to close the network Connection.<br>The HTTP connection uses the "request-response" approach (2 handshake), not only to establish a connection at the time of the request, but also to request the server to reply to the Server. The socket connection can transmit data directly after the connection is Established.<br>Features of the HTTP Protocol:<br>Support B/S and c/s mode;<br>Simple and fast: when a customer requests a service from the server, it simply transmits the request method and Path. The request method commonly has, POST.<br>Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by content-type;<br>Stateless: The HTTP protocol is a stateless Protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to Increase.<br>HTTP protocol Request Method:<br>The request method is included in the request line, as explained Below:<br>Get request gets the resource identified by request-uri;<br>Post appends new data to the resource identified by the request-uri;<br>Head request Gets the response message header for the resource identified by Request-uri<br>Put request server stores a resource and uses Request-uri as its identity<br>Delete Requests the server to remove the resources identified by the request-uri;<br>Trace requests the server to echo received request information, primarily for testing or diagnostics<br>Connect reserved for future use<br>Options request the performance of the query server, or query for resource-related choices and requirements<br><br><br>The HTTP communication method is also divided into two ways of Get and post:<br>The POST request can transmit data to the server, and the data is sent to the Service-side URL address together with the htmlheader, and the data is not visible to the User. The get is to add the parameter data queue to the URL of the submission, the value and the form in each field one by one corresponding. Post Security is High.<br>Get transmits a small amount of data and cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by Default. In theory, however, the maximum amount of IIS4 in 80KB,IIS5 is 100kb,t security is very low.<br>Get mode:</p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=1&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static InputStream getInputStream (String Urlpath) {</span></span></li></li> <li><li>InputStream InputStream = <span class="keyword">null; </span></li></li> <li class="alt"><li class="alt"><span class="keyword">try {</span></li></li> <li><li><span class="comment">//parameter Stitching in URLPath</span></li></li> <li class="alt"><li class="alt">URL url = <span class="keyword">new URL (urlpath); </span></li></li> <li><li>HttpURLConnection conn = (httpurlconnection) url.openconnection ();</li></li> <li class="alt"><li class="alt">Conn.setrequestmethod (<span class="string">"GET"); </span></li></li> <li><li>Conn.setconnecttimeout (<span class="number">5 * <span class="number">1000); </span></span></li></li> <li class="alt"><li class="alt">Conn.setrequestproperty (<span class="string">"charsert", <span class="string">"utf-8"); </span></span></li></li> <li><li><span class="keyword">if (conn.getcontentlength () ><span class="number">0) {</span> </span></li></li> <li class="alt"><li class="alt">InputStream = Conn.getinputstream (); <span class="comment">//get input Stream</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">} <span class="keyword">catch (Exception e) {</span></li></li> <li><li><span class="comment">//TODO auto-generated Catch block</span></li></li> <li class="alt"><li class="alt">LOG4DEBUG.E (TAG, <span class="string">"getinputstream", e.getmessage ()); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"><span class="keyword">return inputstream; </span></li></li> <li><li>}</li></li> </ol><p><p>Post Mode:</p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=2&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static InputStream getInputStream (String Urlpath) {</span></span></li></li> <li><li>InputStream InputStream = <span class="keyword">null; </span></li></li> <li class="alt"><li class="alt"><span class="keyword">try {</span></li></li> <li><li>URL url = <span class="keyword">new URL (urlpath); </span></li></li> <li class="alt"><li class="alt">HttpURLConnection conn = (httpurlconnection) url.openconnection ();</li></li> <li><li>Conn.setconnecttimeout (<span class="number">5 * <span class="number">1000); </span></span></li></li> <li class="alt"><li class="alt">Conn.setrequestproperty (<span class="string">"charsert", <span class="string">"utf-8"); </span></span></li></li> <li><li>Conn.setdoinput (<span class="keyword">true); </span></li></li> <li class="alt"><li class="alt">Conn.setdooutput (<span class="keyword">true); </span></li></li> <li><li>Conn.setrequestmethod (<span class="string">"POST"); </span></li></li> <li class="alt"><li class="alt">Conn.setusecaches (<span class="keyword">false); </span></li></li> <li><li>Conn.setrequestproperty (<span class="string">"content-type", <span class="string">"application/x-www-form-urlencoded"); </span></span></li></li> <li class="alt"><li class="alt">DataOutputStream outputstream = <span class="keyword">new DataOutputStream (conn.getoutputstream ()); </span></li></li> <li><li><span class="comment">//write Parameters</span></li></li> <li class="alt"><li class="alt">Outputstream.writebytes ("<span class="string">param=" +urlencoder.encode (<span class="string">"value")); </span></span></li></li> <li><li>Outputstream.flush ();</li></li> <li class="alt"><li class="alt">Outputstream.close ();</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">if (conn.getcontentlength () ><span class="number">0) {</span> </span></li></li> <li><li>InputStream = Conn.getinputstream (); <span class="comment">//get input Stream</span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>} <span class="keyword">catch (Exception e) {</span></li></li> <li class="alt"><li class="alt"><span class="comment">//TODO auto-generated Catch block</span></li></li> <li><li>LOG4DEBUG.E (TAG, <span class="string">"getinputstream", e.getmessage ()); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li><span class="keyword">return inputstream; </span></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p class="java"><p class="java">2. Connect to the server using the Apache HTTP interface</p></p><p class="java"><p class="java"><1> Get mode:</p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=3&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static InputStream dogetforstream (String urlpath,map<string, string> params,string Encoding) {</span></span></li></li> <li><li>StringBuilder builder = <span class="keyword">new StringBuilder (urlpath); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">try {</span></li></li> <li><li><span class="keyword">if (params! =<span class="keyword">Null) {</span> </span></li></li> <li class="alt"><li class="alt">Builder.append (<span class="string">"?"); </span></li></li> <li><li>iterator<?> Iterator = Params.entryset (). Iterator ();</li></li> <li class="alt"><li class="alt">while <span class="keyword">(iterator.hasnext ()) {</span></li></li> <li><li><span class="annotation">@SuppressWarnings (<span class="string">"unchecked")</span> </span></li></li> <li class="alt"><li class="alt">map.entry<string,string> Entry = (map.entry<string,string>) Iterator.next ();</li></li> <li><li>Builder.append (entry.getkey ()). Append (<span class="string">"="). append (entry.getvalue ()). append (<span class="string">"&"); </span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>Builder.deletecharat (builder.length ()-<span class="number">1); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>HttpGet httpget = <span class="keyword">new HttpGet (builder.tostring ()); </span></li></li> <li class="alt"><li class="alt">Httpparams httpparams = <span class="keyword">new Basichttpparams (); </span></li></li> <li><li>Httpparams.setparameter (<span class="string">"charset", encoding); </span></li></li> <li class="alt"><li class="alt">Httpget.setparams (httpparams);</li></li> <li><li>HttpResponse HttpResponse = <span class="keyword">new Defaulthttpclient (). Execute (httpget); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">if (httpresponse!= <span class="keyword">null && httpresponse.getstatusline (). getstatuscode () = <span class="number"></span> =</span> </span> () {</li></li> <li><li><span class="keyword">return httpresponse.getentity (). getcontent (); </span></li></li> <li class="alt"><li class="alt">}<span class="keyword">Else {</span></li></li> <li><li><span class="keyword">return <span class="keyword">null; </span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>} <span class="keyword">catch (Exception e) {</span></li></li> <li class="alt"><li class="alt"><span class="comment">//TODO auto-generated Catch block</span></li></li> <li><li>LOG4DEBUG.E (TAG, <span class="string">"dogetforstream", e.getmessage ()); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">return <span class="keyword">null; </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p><2> Post Mode:</p></p><strong><strong>[java]</strong></strong><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">View Plaincopy</span></span></span><object id="ZeroClipboardMovie_4" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_4" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="flashvars" value="id=4&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-j"> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">static InputStream dopostforstream (String urlpath,list<namevaluepair> params,string Encoding) { </span></span></li></li> <li><li>HttpPost HttpPost = <span class="keyword">new HttpPost (urlpath); </span></li></li> <li class="alt"><li class="alt"><span class="keyword">try {</span></li></li> <li><li><span class="keyword">if (params! = <span class="keyword">Null) {</span> </span></li></li> <li class="alt"><li class="alt">Httppost.setentity (<span class="keyword">new urlencodedformentity (params,encoding)); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">HttpResponse HttpResponse = <span class="keyword">new Defaulthttpclient (). Execute (httppost); </span></li></li> <li><li><span class="keyword">if (httpresponse!= <span class="keyword">null && httpresponse.getstatusline (). getstatuscode () = <span class="number"></span> =</span> </span> () {</li></li> <li class="alt"><li class="alt"><span class="keyword">return httpresponse.getentity (). getcontent (); </span></li></li> <li><li>}<span class="keyword">Else {</span></li></li> <li class="alt"><li class="alt"><span class="keyword">return <span class="keyword">null; </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">} <span class="keyword">catch (Exception e) {</span></li></li> <li><li><span class="comment">//TODO auto-generated Catch block</span></li></li> <li class="alt"><li class="alt">LOG4DEBUG.E (TAG, <span class="string">"dopostforstream", e.getmessage ()); </span></li></li> <li><li><span class="keyword">return <span class="keyword">null; </span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> </ol><p><p>Android HTTP Communication</p></p></span>
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