The content-tpye of the request on the server side is multipart/form-data, how do I send the data to request? Using afnetworking

  ios, question

Just like the topic
What should I do?

This was the case when asi was used before.

NSDictionary *sinaDBDictionary = [sinaDBArray objectAtIndex:0];
 NSString *accessTokenKey = [sinaDBDictionary objectForKey:@"AccessTokenKey"];
 NSString *wbUid = [sinaDBDictionary objectForKey:@"UserIDKey"];
 
 NSString *boundary = @"14745591349540787582088777204";
 
 
 //Set the content in the request body
 NSMutableString *bodyString = [NSMutableString string];
 [bodystring appendformat: @ "-percentage @\r\n", boundary];
 [bodyString appendString:@"Content-Disposition: form-data;   name=\"accessToken\"\r\n"];
 [bodyString appendString:@"\r\n"];
 [bodyString appendString:accessTokenKey];
 [bodyString appendString:@"\r\n"];
 [bodystring appendformat: @ "-percentage @\r\n", boundary];
 [bodyString appendString:@"Content-Disposition: form-data;   name=\"wbUid\"\r\n"];
 [bodyString appendString:@"\r\n"];
 [bodyString appendString:wbUid];
 [bodyString appendString:@"\r\n"];
 [bodystring appendformat: @ "-percentage @-\ r \ n", boundary];
 
 NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
 NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
 [bodyData appendData:bodyStringData];
 
 Nsstring * length = [nsstringstringwithformat: @ "percentage d",[bodyData length]];
 
 NSString *s = [NSString stringWithFormat:@"multipart/form-data;  Boundary =------------%@", boundary];
 
 
 NSString *url = @ "mask mask mask mask";
 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: url]];
 request.defaultResponseEncoding = NSUTF8StringEncoding;
 request.useCookiePersistence = YES;
 [request setRequestMethod:@"POST"];
 [request addRequestHeader:@"Content-Type" value:s];
 [request addRequestHeader:@"Content-Length" value:len];
 [request setPostBody:bodyData];
 [request startSynchronous];
 
 NSError *error = request.error;
 if (!  error) {
 // NSLog(@ "Percent @, [RequestResponse String]);
 }else{
 // NSLog(@ "Percentage @", [[RequestError] Localized Description]);
 bracket

It’s really complicated

And now it’s time to switch to afn. What should I do?

I wrote a category to set the request:

//
 //nsmutableulrequest plus FormData.m
 //  Secrets
 //
 //  Created by ysq on 14-1-28.
 // Copyright (c) 2014 ysq. All rights reserved.
 //
 
 # import "nsmutableurrequestplus FormData.h"
 
 @implementation NSMutableURLRequest (FormData)
 
 
 
 Add (NSString *)boundaryData{
 return @"12436041281943726692693274280";
 bracket
 -(void)setFormData:(NSDictionary *)formData{
 
 NSString *boundary = [NSMutableURLRequest boundaryData];
 
 //Set the content in the request body
 NSMutableString *bodyString = [[NSMutableString alloc]init];
 
 
 for (int i=[[formData allKeys]count]-1;   i>=0;  i--) {
 
 NSString *key = [formData allKeys][i];
 NSString *value = [formData allValues][i];
 if ([key isEqualToString:@"accessToken"]) {
 value = [value substringToIndex:32];
 bracket
 
 [bodystring appendformat: @ "-percentage @ \ r \ ncontent-disposition: form-data;  Name=\ "Percent @\"\r\n\r\n Percent @\r\n",boundary,key,value];
 bracket
 
 [bodystring appendformat: @ "-percentage @-\ r \ n", boundary];
 
 
 NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
 NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
 [bodyData appendData:bodyStringData];
 
 Nsstring * contentlength = [nsstringstringwith format: @ "percentage d",[bodyData length]];
 
 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;  Boundary =------------%@", boundary];
 
 
 
 [self setValue:contentType forHTTPHeaderField:@"Content-Type"];
 [self setValue:contentLength forHTTPHeaderField:@"Content-Length"];
 [self setHTTPBody:bodyData];
 [self setHTTPMethod:@"POST"];
 
 bracket
 
 
 
 @end