0 , in understanding HTTP before you know URL
Before we know HTTP, it is necessary to figure out the composition of the URL, such as:
Http://www.******.com/china/index.htm
The meaning of it is as follows:
1./http: Represents Hypertext Transfer Protocol, notifies ****.comserver to display Web pages, 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 name of the Web server;
4.china/: As a subfolder on the server, just like our folder;
5.index.htm:index.htm is an HTML file (Web page) in the directory.
1 , what is HTTP
HTTP protocol is an application-level protocol HTTP is a hypertexttransfer Protocol ( Hypertext Transfer Protocol ) abbreviation for English.
HTTP can be passed through the transport Layer TCP the protocol transfers data between the client and the server.
HTTP The protocol is mainly used for Web Browser and Web exchange of data between servers.
When we use IE or Firefox to browse the Web or download Web resources. By entering in the address bar, the first 4- Letter http is equivalent to notifying the browser to use the HTTP protocol to Host determines the server to communicate.
http/1.0
This is the first HTTP protocol version number that specifies the version number in the newsletter and is still widely used today. In particular, in proxy server.
http/1.1
The current version number. Persistent connections are used by default. and can work very well with Proxy server.
It also supports the pipeline to send multiple requests at the same time in order to reduce line load and increase transmission speed.
2 , HTTP how it works
HTTP the protocol uses the request / how the response works. after each request to the server by the client based on the HTTP1.0 Protocol, the server returns a response message to the client (with the correct request and the requested data), after confirming that the client has received a response message , the server shuts down the network connection (in effect shutting down the TCP connection). During this transfer of data. theHTTP Protocol is also considered a stateless protocol, regardless of historical information and status information. Figure 1 depicts the communication process for the HTTP1.0 protocol.
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbgluzmvuzzi0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
3 , HTTP Response Code
1XX: Information response class, indicating receipt of request and continuation of 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 includes syntax error or is not running correctly
5XX: Server fails to correctly run a correct request
4 , HTTP the request body and the reply body
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbgluzmvuzzi0/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
5 , two kinds of HTTP Communication
( 1 ) HttpURLConnection
( 2 ) httpclienturlconnection
The following examples illustrate the first type of httpurlconnection
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 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. Indicates 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 write to the local file while (length < currentpartsize&& (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 ();}}}
Internet HTTP communication for Android notes