Tag: Win 5.0 Val has das height HTTP request Handle Request method
HTTP request messages consist of request lines, request headers, spaces, and request data.
1. Request line consists of request method space Request URL space version
2. The request header consists of a keyword/value
Commonly used by user-agent: Browser type
Accept: List of types that clients can recognize
Host: hostname
3. Spaces: distinguish between different parts
4. Only with Post: the most frequently requested tail content-length,content-type
5.get Request Instance
//Request First line get/hello/index.jsp http/1.1//request header information because the GET request has no body host:localhostuser-agent:mozilla/5.0 (Windows NT 5.1; RV : 5.0) gecko/20100101 firefox/5.0accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/* ; Q=0.8accept-language:zh-cn,zh;q=0.5accept-encoding:gzip, deflateaccept-charset:gb2312,utf-8;q=0.7,*;q= 0.7connection:keep-alivecookie:jsessionid=369766fdf6220f7803433c0b2de36d98//empty Line//Because get has no body, so the following is empty
6.post Request Instance
Request first line post/hello/index.jsp http/1.1//request header information host:localhostuser-agent:mozilla/5.0 (Windows NT 5.1; rv:5.0) gecko/ 20100101 FIREFOX/5.0ACCEPT:TEXT/HTML,APPLICATION/XHTML+XML,APPLICATION/XML;Q=0.9,*/*;Q=0.8ACCEPT-LANGUAGE:ZH-CN , Zh;q=0.5accept-encoding:gzip, Deflateaccept-charset:gb2312,utf-8;q=0.7,*;q=0.7connection:keep-alivereferer: http://localhost/hello/index.jspCookie:JSESSIONID=369766FDF6220F7803433C0B2DE36D98Content-Type:application/ x-www-form-urlencoded content-length:14//Here is a blank line//post has the request body Username=hello
7.URLConnection Implement file Upload (Note request header)
OK first look at this diagram, upload the file and the normal POST request is not the same, it has some of this, however, there is a need for us to splice the content, divided into two parts above the Content-type is in our request in the head of those who write the red word is that we requested to splice the number of lines to change , I also pieced together, you can join the words with the following figure step by step stitching.
1, determine the URL nsurl *url = [Nsurl urlwithstring:@ "Http://192.168.1.0:8080/upload"]; 2, determine the request nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; /******************************************************************///Setting the request header [requesting Setv alue:@ "Multipart/form-data; boundary=----WEBKITFORMBOUNDARYUFNAH6LOSNXU4XDQ "forhttpheaderfield:@" Content-type "]; /******************************************************************///Set request body//Set Request body/ /Add fixed format data to the request body here is also the use of variable, because more well nsmutabledata *data = [Nsmutabledata data]; /******************************************************************///start tag [data appendData: [@ "------WEBKITFORMBOUNDARYUFNAH6LOSNXU4XDQ" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "content-disposition:form-data; name=\" file\ "; filename=\" liiii.png\ "" DatausiNgencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "Content-type:image/png" datausingencoding:nsutf8stringencoding]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; /******************************************************************///Upload file parameters//Picture data and convert to Dat a UIImage *image = [UIImage imagenamed:@ "LIIII"]; NSData *imagedata = uiimagepngrepresentation (image); [Data Appenddata:imagedata]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; /******************************************************************///non-file parameters [data appendData : [@ "------WEBKITFORMBOUNDARYUFNAH6LOSNXU4XDQ" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" DatausingencOding:nsutf8stringencoding]]; [Data appenddata:[@ "content-disposition:form-data; name=\" username\ "" datausingencoding:nsutf8stringencoding] "; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "Litterl" datausingencoding:nsutf8stringencoding]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; /******************************************************************///Add end tag [data appendData : [@ "------webkitformboundaryufnah6losnxu4xdq--" datausingencoding:nsutf8stringencoding]]; [Data appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]]; /******************************************************************///Request method. HttpMethod = @ "POST"; Request body. Httpbody = data; 3. Send Request [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( NsurlrespoNSE * _nullable response, NSData * _nullable data, Nserror * _nullable connectionerror) {NSLog (@ "%@", [[NSString Al Loc]initwithdata:data encoding:nsutf8stringencoding]); }];
iOS Development Network Detailed (iii)---HTTP request messages and response messages (get and post)