關於iPhone應用中HTTP上傳請求協議總結

來源:互聯網
上載者:User

關於iPhone應用中HTTP上傳請求協議總結是本文要介紹的內容,主要是來學習HTTP上傳請求協議的內容,具體內容來看本文詳解。http協議應該注意四點:

1、初始化響應伺服器位址

 
  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"]]; //可要可無 

2、設定提交方法  GET/POST

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

3、設定響應內容類型

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

4、設定響應內容

 
  1. NSMutableData *postData = [NSMutableData dataWithCapacity:[m_imageData length]];  
  2. [postData appendData: [[NSString stringWithFormat:@"---------%@\r\n", @"7daaba1e0368"]  
  3.                       dataUsingEncoding:NSUTF8StringEncoding]];//開始標誌  
  4.  
  5. [postData appendData: [[NSString stringWithFormat: @"Content-Disposition:form-data; name="File1";                
  6. filename="1.jpg"\r\n Content-type: image/pjpeg\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];  
  7. //name是分頁檔的參數,type是檔案類型  
  8. [postData appendData:imageData];// 檔案資料  
  9. [postData appendData: [[NSString stringWithFormat:@"\r\n---------%@--\r\n",  @"7daaba1e0368"]  
  10.                       dataUsingEncoding:NSUTF8StringEncoding]];// 檔案結束標誌  
  11. [urlRequest setHTTPBody:postData];//把資料載入到響應的檔案體 

只要做到以上幾點,一個完整的http請求協議就做好了

下面附一個簡潔版完整的請求協議體

 
  1. NSURL *url = [NSURL URLWithString:@"http://XX.XX.XX"];//請求伺服器路徑。  
  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\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\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];  

小結:關於iPhone應用HTTP上傳請求協議總結的內容介紹完了,希望通過本文的學習能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.