1 The code in Viewcontroller is as follows2 3- (void) Viewdidload {4 [Super Viewdidload];5 6NSString *URLSTR1 =@"http://localhost/cgi-bin/post1.cgi";7NSString *URLSTR2 =@"http://localhost/cgi-bin/post2.cgi";8 9 //[self sendpost1:urlstr1];Ten [self sendpost2:urlstr2]; One } A - #pragmaMark-send POST 2- - /** the use NSData (binary data) to host request information and upload files more - */ --(void) SendPost2: (nsstring*) urlstr{ - //preparatory work (in a well-defined format) +NSString *head =@"Content-type:multipart/form-data, boundary=aab03x\n\n"; - //Set delimiter +NSString *boundary =@"aab03x"; A //the delimiter of the head atNSString *headboundary = [NSString stringWithFormat:@"--%@\n", boundary]; - //the trailing delimiter -NSString *endboundary = [NSString stringWithFormat:@"\n--%@--\n", boundary]; - - //encapsulating the request header -NSString *contentstring =@"content-disposition:form-data; name=\"Pic\"; filename=\ "star_pressed.png\" content-type:image/png\n\n"; in - //Create request toNsurl *url =[Nsurl Urlwithstring:urlstr]; +Nsmutableurlrequest *request =[[Nsmutableurlrequest Alloc]initwithurl:url]; - the //Action on request *[Request Sethttpmethod:@"POST"]; $ //format the uploaded dataPanax Notoginseng[Request AddValue:@"Multipart/form-data"Forhttpheaderfield:@"Content-type"]; - the //Set Body +NSData *filedata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"star_pressed"OfType:@"PNG"]]; A the //set the total request Body +Nsmutabledata *data =[Nsmutabledata data]; - $ //Header Text = header + Header separator + content description + Body entity + Tail Separator $ - [Data Appenddata:[head datausingencoding:nsutf8stringencoding]; - [Data appenddata:[headboundary datausingencoding:nsutf8stringencoding]; the [Data appenddata:[contentstring datausingencoding:nsutf8stringencoding]; - [Data appenddata:filedata];Wuyi [Data appenddata:[endboundary datausingencoding:nsutf8stringencoding]; the - //set the length of the request data Wu[Request Setvalue:[nsstring stringWithFormat:@"%lu", (unsignedLong) Data.length] Forhttpheaderfield:@"Content-length"]; - About [Request Sethttpbody:data]; $ - //Create request, wait for return -_connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: self]; - } A + #pragmaMark-send POST 1- the /** - get similar, use URL to host request information, more for login registration, fill in the form and other operations (this way of POST request can also be implemented with GET request, but GET request, send data is bare) $ */ the-(void) SendPost1: (nsstring*) urlstr{ theNsurl *url =[Nsurl Urlwithstring:urlstr]; the theNsmutableurlrequest *request =[[Nsmutableurlrequest Alloc]initwithurl:url]; - in //specify the request method as a POST request the[Request Sethttpmethod:@"POST"]; the //the data to be uploaded AboutNSString *poststr =@"Username=jingjing&password=woxiangjingjing&message=yueme"; the //Convert to binary data theNSData *postdata =[Poststr datausingencoding:nsutf8stringencoding]; the //set the request body + [Request Sethttpbody:postdata]; - the //set the length of the request dataBayi[Request Setvalue:[nsstring stringWithFormat:@"%lu", (unsignedLong) Postdata.length] Forhttpheaderfield:@"Content-length"]; the the //format the uploaded data -[Request AddValue:@"application/x-www-form-urlencoded"Forhttpheaderfield:@"Content-type"]; - the //Create request, wait for return the_connection = [[Nsurlconnection alloc]initwithrequest:requestDelegate: self]; the } the - the the #pragmaMark-Proxy Method- the 94- (void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{ the theNSLog (@"receiving the response header"); theNSLog (@"response:%@", response);98 About if(_data = =Nil) { -_data =[[Nsmutabledata alloc]init];101 }102_data.length =0;103 }104 the- (void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{106 [_data appenddata:data];107 108NSLog (@"receive data .... :%ld", data.length);109 the }111 the-(void) Connectiondidfinishloading: (Nsurlconnection *) connection{113NSString *str =[[NSString alloc]initwithdata:_data encoding:nsutf8stringencoding]; theNSLog (@"receive data complete: \n%@", str); the } the 117-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{118NSLog (@"the error:%@", error);119 } - 121 122 123- (void) didreceivememorywarning {124 [Super didreceivememorywarning]; the //Dispose of any resources the can be recreated.126}View Code
Two ways to post requests