Internet HTTP communication for Android notes

Source: Internet
Author: User

0 , in understanding HTTP before you know URL

Before we know HTTP, it is necessary to figure out the composition of the URL, for example:

Http://www.******.com/china/index.htm

It has the following meanings:

1./http: Represents the Hypertext Transfer Protocol, notifying the ****.com server that the Web page is displayed, usually without input;

2. WWW: Represents a Web (WWW) server;

3.****.com/: This is the domain name of the server that contains the Web page, or the names of the site servers;

4.china/: As a subdirectory on the server, it is like our folder;

5.index.htm:index.htm is an HTML file (Web page) in a folder.

1 , what is HTTP

HTTPprotocol is an application-level protocolHTTPis aHypertexttransfer Protocol (Hypertext Transfer Protocol)abbreviation for English. HTTPcan be passed through the transport layer'sTCPthe protocol transmits data between the client and the server. HTTPThe protocol is mainly used forWebBrowser andWebexchange of data between servers. We are usingIEorFirefoxbrowse the Web or downloadWebresource, by entering in the address bar, the beginning of the4a letterhttpis equivalent to notifying the browser to useHTTPagreement to andHostThe identified server for communication.

http/1.0

This is the first HTTP protocol version that specifies the version number in the newsletter, and is still widely used, especially in proxy servers.

http/1.1

The current version. Persistent connections are adopted by default and work well with proxy servers. It also supports the pipeline to send multiple requests simultaneously in order to reduce the line load and increase the transfer speed.

2 , HTTP Working Style

HTTP the agreement takes the request / how the response works. after each request to the server, the HTTP1.0 protocol-based client returns a response message to the client, including the correct request and the requested data, after confirming that the client has received a response message. The server shuts down the network connection (actually shutting down the TCP connection). In this data transfer process, no historical information and status information is saved, so theHTTP Protocol is also considered a stateless protocol, figure 1 depicts the HTTP1.0 The communication process of the Protocol.



3 , HTTP Response Code

1XX: Information response class, which indicates receipt of request and continues processing

2XX: Handle the successful response class, indicating that the action was successfully received, understood, and accepted

3XX: Redirect Response class, must accept further processing in order to complete the specified action

4XX: Client error, client request contains syntax error or is not executed correctly

5XX: Server error, servers do not correctly execute a correct request

4 , HTTP the request body and the reply body




5 , two kinds of HTTP Communication

( 1 ) HttpURLConnection

( 2 ) httpclienturlconnection


The first type of httpurlconnection is illustrated below

Private class Downthread extends thread{//the current thread's download location private int startpos;//Defines the file size that the current thread is responsible for downloading private int currentpartsize;/ /When the front-thread needs to download the file Block private Randomaccessfile currentpart;//defines the number of bytes that have been downloaded by this thread public int length;public downthread (int startpos, int Currentpartsize,randomaccessfile currentpart) {this.startpos = Startpos;this.currentpartsize = CurrentPartSize; This.currentpart = Currentpart;} @Overridepublic void Run () {try{url url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setconnecttimeout (5 * 1000); Conn.setrequestmethod ("GET"); Conn.setrequestproperty ("Accept", "Image/gif, Image/jpeg, Image/pjpeg, image/pjpeg," + "Application/x-shockwave-flash, Application/xaml+xml," + "application/vnd.ms-xpsdocument, APPLICATION/X-MS-XBAP," + "Application/x-ms-application, Application/vnd.ms-excel," + "Application/vnd.ms-powerpoint, Application/msword, */* "); Conn.setrequestproperty (" Accept-language "," ZH-CN "); Conn.setrequestproperty (" Charset "," UTF-8 "); InputStream Instream = Conn.getinputstream ();//skips startpos bytes, indicating that the thread only downloads which part of the file it is responsible for. Instream.skip (This.startpos); byte[] buffer = new Byte[1024];int Hasread = 0;//Reads network data and writes to local file while (length < CURRENTPA rtsize&& (Hasread = instream.read (buffer)) > 0) {currentpart.write (buffer, 0, hasread);//Cumulative total size of the thread download length + = Hasread;} Currentpart.close (); Instream.close ();} catch (Exception e) {e.printstacktrace ();}}}








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.