The same as taking the data.
First, define a dictionary to hold the parameters
nsdictionary* params = [nsdictionary dictionary];
Params[@ "Property"] = value;
Uploading string data:
// Create a Request management class afhttprequestoperationmanager* mgr = [Afhttprequestoperationmanager manager]; // Send Request [Mgr Post:url parameters:params success:ID responseobject) { // successful callback } failure:^ (afhttprequestoperation *operation, Nserror *error) { // failure callback } ];**
Upload the file data (convert the file to binary database in advance):
//Create a Request management classafhttprequestoperationmanager* mgr =[Afhttprequestoperationmanager manager];//Send Request[Mgr Post:url parameters:paramsconstructingbodywithblock:^ (ID<AFMultipartFormData>totalformdata) {[Totalformdata appendPartWithFileData:formData.data name:formData.name fileName:formData.fileName Mimetype:formda Ta.mimetype];} Success:^ (Afhttprequestoperation *operation,IDresponseobject) { //Successful callback} failure:^ (Afhttprequestoperation *operation, Nserror *error) {//Failure Callback} ];
No third party, write yourself
First, send JSON data to the server
Steps to send JSON data to the server:
(1) Be sure to use the POST request
(2) Set the request header
(3) Set the JSON data as the request body
code example:
1#import"YYViewController.h"23@interfaceYyviewcontroller ()45@end67@implementationYyviewcontroller89-(void) Viewdidload10{11[Super Viewdidload];12}1314-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event15{16//1. Create a requestNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/MJServer/order"];Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];Request. HttpMethod =@"POST";2021st//2. Set the request header[Request SetValue:@"Application/json"Forhttpheaderfield:@"Content-type"];2324//3. Set the request bodyNsdictionary *json =@{26@"order_id" :@"123",27@"user_id" :@"789",28@"Shop" :@"Toll"29};3031//NSData-Nsdictionary32 // Nsdictionary-- > Nsdata33 nsdata *data = [nsjsonserialization Datawithjsonobject:json options:nsjsonwritingprettyprinted Error:nil]; 34 request. Httpbody = Data; 36 //4. Send Request PNS [nsurlconnection Sendasynchronousrequest:request Queue:[nsoperationqueue Mainqueue] completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {NSLog (@ "%d", data.length); 39}]; 40 }41 42 @end
Two, multi-value parameters
Multivalued parameter: One parameter corresponds to multiple values.
Request parameters such as the following:
Http://192.168.1.103:8080/MJServer/weather?place= Beijing &place= Henan &place= Hunan
The place property of the server is an array. Therefore, using the same parameter does not overwrite the value of the server.
iOS development-afnetworking How to upload data? How to upload a mock-up form