IPhoneApplication in progressHTTPThe upload request protocol is the content to be introduced in this article. Recently, a project involves image uploading. At first, socket is used, but it is used because it is unfamiliar.HTTPThe protocol is used for web upload, which must be customized here.HTTPProtocol.
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, 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: DetailsIPhoneApplication in progressHTTPUploadRequest ProtocolThe contentHttpUploadRequest ProtocolFinally, I hope this article will help you!