Summary of HTTP upload request protocol in iPhone applications

Source: Internet
Author: User

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

 
 
  1. NSURL * url = [NSURL URLWithString: @ "http://xxx.xxx.xx.xxx/mqupload.jsp"];
  2.  
  3. [UrlRequest setValue: [NSString stringWithFormat: @ "% @ \ r \ n", @ "http: // xxxxxx http/1.1"]; // optional

2. Set the submit method GET/POST.

 
 
  1. [urlRequest setHTTPMethod:@"POST"]; 

3. Set the response content type

 
 
  1. [urlRequest setValue:  [NSString stringWithFormat:@"multipart/form-data;      
  2.  boundary=---------%@", @"7daaba1e0368"] forHTTPHeaderField:@"Content-Type"]; 

4. Set the response content

 
 
  1. NSMutableData * postData = [NSMutableData dataWithCapacity: [m_imageData length];
  2. [PostData appendData: [[NSString stringWithFormat: @ "--------- % @ \ r \ n", @ "7daaba1e0368"]
  3. DataUsingEncoding: NSUTF8StringEncoding]; // start flag
  4.  
  5. [PostData appendData: [[NSString stringWithFormat: @ "Content-Disposition: form-data; name =" File1 ";
  6. Filename = "1.jpg" \ r \ n Content-type: image/pjpeg \ r \ n"] dataUsingEncoding: NSUTF8StringEncoding];
  7. // Name is the parameter of the page file, and type is the file type
  8. [PostData appendData: imageData]; // file data
  9. [PostData appendData: [[NSString stringWithFormat: @ "\ r \ n --------- % @ -- \ r \ n", @ "7daaba1e0368"]
  10. DataUsingEncoding: NSUTF8StringEncoding]; // end of the file
  11. [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

 
 
  1. NSURL * url = [NSURL URLWithString: @ "http://XX.XX.XX"]; // request the server path.
  2. M_imageData = UIImagePNGRepresentation (myImageView. image );
  3. NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL: url];
  4. [UrlRequest setHTTPMethod: @ "POST"];
  5. [UrlRequest setValue: [NSString stringWithFormat: @ "multipart/form-data;
  6. Boundary = --------- % @ ", @" 7daaba1e0368 "] forHTTPHeaderField: @" Content-Type "];
  7. [UrlRequest setValue: [NSString stringWithFormat: @ "% @ \ r \ n", @ "keep-alive"] forHTTPHeaderField: @ "Connection"];
  8. NSMutableData * postData = [NSMutableData dataWithCapacity: [m_imageData length];
  9. [PostData appendData: [[NSString stringWithFormat: @ "--------- % @ \ r \ n", @ "7daaba1e0368"]
  10. DataUsingEncoding: NSUTF8StringEncoding];
  11.  
  12. [PostData appendData: [[NSString stringWithFormat:
  13. @ "Content-Disposition: form-data; name =" File1 ";
  14. Filename = "1.jpg" \ r \ n Content-type: image/pjpeg \ r \ n"] dataUsingEncoding: NSUTF8StringEncoding];
  15. [PostData appendData: m_imageData];
  16. [PostData appendData: [[NSString stringWithFormat: @ "\ r \ n --------- % @ -- \ r \ n", @ "7daaba1e0368"]
  17. DataUsingEncoding: NSUTF8StringEncoding];
  18. [UrlRequest setHTTPBody: postData];
  19. NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest: urlRequest delegate: self];

Summary: AboutIPhone applicationsMediumHTTPUploadRequest ProtocolThe summary is complete. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.