我是 ios webservices 的新手,所以在过去的 2 到 3 天里我遇到了一个问题,我必须向服务器发送一个请求,它有多个部分,如图像和 json,我尝试使用 multipart/form-data 来发送我的请求,但由于某种原因服务器无法获得请求,谁能帮我解决这个问题
我正在使用的代码是
NSData *imageData = UIImagePNGRepresentation(img);
NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:theURL];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
NSMutableString *theBody = [[NSMutableString alloc]init];
[theBody appendString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary]];
[theBody appendString:[NSString stringWithFormat:@"Content-Type: application/json\r\n\r\n"]];
//append The Json string
[theBody appendString:myJsonString];
[theBody appendString:[NSString stringWithFormat:@"%@", boundary]];
//this appends the image
[theBody appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"data\"; filename=\"photo\""]];
[theBody appendString:[NSString stringWithFormat:@"Content-Type: image/png\r\n\r\n"]];
[theBody appendString:[NSString stringWithFormat:@"%@",imageData]];
[theBody appendString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] ];
[theRequest setHTTPBody:[theBody dataUsingEncoding:NSUTF8StringEncoding]];