AboutHTTP in iPhone applicationsUploadRequest ProtocolThe summary is the content to be introduced in this article, mainly to learnHTTPUploadRequest ProtocolFor details, refer to this article.HttpNote the following four points for the agreement:
1. initialize the response server address
- NSURL * url = [NSURL URLWithString: @ "http://xxx.xxx.xx.xxx/mqupload.jsp"];
-
- [UrlRequest setValue: [NSString stringWithFormat: @ "% @ \ r \ n", @ "http: // xxxxxx http/1.1"]; // optional
2. Set the submit method GET/POST.
- [urlRequest setHTTPMethod:@"POST"];
3. Set the response content type
- [urlRequest setValue: [NSString stringWithFormat:@"multipart/form-data;
- boundary=---------%@", @"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"];
4. Set the response content
- NSMutableData * postData = [NSMutableData dataWithCapacity: [m_imageData length];
- [PostData appendData: [[NSString stringWithFormat: @ "--------- % @ \ r \ n", @ "7daaba1e0368"]
- DataUsingEncoding: NSUTF8StringEncoding]; // start flag
-
- [PostData appendData: [[NSString stringWithFormat: @ "Content-Disposition: form-data; name =" File1 ";
- Filename = "1.jpg" \ r \ n Content-type: image/pjpeg \ r \ n"] dataUsingEncoding: NSUTF8StringEncoding];
- // Name is the parameter of the page file, and type is the file type
- [PostData appendData: imageData]; // file data
- [PostData appendData: [[NSString stringWithFormat: @ "\ r \ n --------- % @ -- \ r \ n", @ "7daaba1e0368"]
- DataUsingEncoding: NSUTF8StringEncoding]; // end of the file
- [UrlRequest setHTTPBody: postData]; // load data to the response file body
A complete http request protocol is ready as long as the preceding settings are met.
A complete request protocol is provided below
- NSURL * url = [NSURL URLWithString: @ "http://XX.XX.XX"]; // request the server path.
- M_imageData = UIImagePNGRepresentation (myImageView. image );
- NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL: url];
- [UrlRequest setHTTPMethod: @ "POST"];
- [UrlRequest setValue: [NSString stringWithFormat: @ "multipart/form-data;
- Boundary = --------- % @ ", @" 7daaba1e0368 "] forHTTPHeaderField: @" Content-Type "];
- [UrlRequest setValue: [NSString stringWithFormat: @ "% @ \ r \ n", @ "keep-alive"] forHTTPHeaderField: @ "Connection"];
- NSMutableData * postData = [NSMutableData dataWithCapacity: [m_imageData length];
- [PostData appendData: [[NSString stringWithFormat: @ "--------- % @ \ r \ n", @ "7daaba1e0368"]
- DataUsingEncoding: NSUTF8StringEncoding];
-
- [PostData appendData: [[NSString stringWithFormat:
- @ "Content-Disposition: form-data; name =" File1 ";
- Filename = "1.jpg" \ r \ n Content-type: image/pjpeg \ r \ n"] dataUsingEncoding: NSUTF8StringEncoding];
- [PostData appendData: m_imageData];
- [PostData appendData: [[NSString stringWithFormat: @ "\ r \ n --------- % @ -- \ r \ n", @ "7daaba1e0368"]
- DataUsingEncoding: NSUTF8StringEncoding];
- [UrlRequest setHTTPBody: postData];
- NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];
Summary: AboutIPhone applicationsMediumHTTPUploadRequest ProtocolThe summary is complete. I hope this article will help you!