2. ASIHttpRequest-發送數據

發送數據

設定request頭

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Referer" value:@"http://www.dreamingwish.com/"];

使用ASIFormDataRequest POST表單

一般數據是以’application/x-www-form-urlencoded’格式發送的,若是上傳了二進制數據或者文件,那麼格式將自動變爲‘multipart/form-data’  web

文件中的數據是須要時才從磁盤加載,因此只要web server能處理,那麼上傳大文件是沒有問題的。 數組

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

數據的mime頭是自動斷定的,可是若是你想自定義mime頭,那麼這樣: app

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];

// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

你可使用addPostValue方法來發送相同name的多個數據(夢維:服務端會以數組方式呈現): url

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

PUT請求、自定義POST請求

若是你想發送PUT請求,或者你想自定義POST請求,使用appendPostData: 或者 appendPostDataFromFile: code

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:@"PUT"];
相關文章
相關標籤/搜索