我尝试使用以下代码>在服务器上保存multipart / form-data;但不保存但Fiddler免费使用save进行调试以获得响应200。如何解决此问题
NSMutableURLRequest * request=[[NSMutableURLRequest
alloc]initWithURL:[NSURL URLWithString:jsonUrl]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"TPop %@",[Base64
encode:[[[NSUserDefaults
standardUserDefaults]objectForKey:@"AuthorizationID"]
dataUsingEncoding:NSUTF8StringEncoding]]]
forHTTPHeaderField:@"Authorization"];
// set Content-Type in HTTP header
NSString *BoundaryConstant = @"---------------------------acebdf13572468";
NSString *contentType= [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
if (parameterArray!=nil)
{
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:[NSString stringWithFormat:@"1"]forKey:[NSString
stringWithFormat:@"SequencePosition"]];
[_params setObject:[NSString stringWithFormat:@"Image"]forKey:[NSString
stringWithFormat:@"MediaType"]];
[_params setObject:[NSString
stringWithFormat:@"Hello"]forKey:[NSString
stringWithFormat:@"Description"]];
[_params setObject:[NSString
stringWithFormat:@"car.jpg"]forKey:[NSString
stringWithFormat:@"filename"]];
for (NSString *param in _params)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",
BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition:
form-data; name=\"%@\"\r\n\r\n", param]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params
objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
UIImageView * imagicon=[[UIImageView
alloc]init];
imagicon.image=[UIImage imageNamed:@"car.jpg"];
NSData *image = UIImageJPEGRepresentation(imagicon.image,1.0); // add image data
if (image)
{
NSString *filename=@"file";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",
BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString
stringWithFormat:@"Content-Disposition:form-data; name=\"%@\";
filename=\"\"\r\n",filename]
dataUsingEncoding:NSUTF8StringEncoding]];
[body
appendData:[@"Content-Type:image/jpeg\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:image];
[body appendData:[[NSString stringWithFormat:@"\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
} [body
appendData:[[NSString stringWithFormat:@"--%@--\r\n",
BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[request
setHTTPBody:body];
NSURLResponse* response; NSLog(@"%@",request);
NSError* error = nil;
//Capturing server response
NSData* result =
[NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error];
NSLog(@"%@",response);
NSLog(@"%@",error);
NSString *responseString = [[NSString alloc]
initWithData:result encoding:NSUTF8StringEncoding];
}
}
获得以下服务器响应
{status code:500,headers {" Cache-Control" =" no-cache&#34 ;; " Content-Length的" = 35; "内容类型" =" application / json; 字符集= UTF-8&#34 ;;日期="星期五,2015年7月31日15:51:01 GMT&#34 ;;到期= " -1&#34 ;; Pragma =" no-cache&#34 ;; Server =" Microsoft-IIS / 8.5&#34 ;; " X-ASPNET-版" =" 4.0.30319&#34 ;; " X供电-通过" =" ASP.NET&#34 ;; }}