获取 System.InvalidOperationException:请求格式无效:通过 AFNetworking 上传图像(数据)时出现多部分/表单数据错误

Posted

技术标签:

【中文标题】获取 System.InvalidOperationException:请求格式无效:通过 AFNetworking 上传图像(数据)时出现多部分/表单数据错误【英文标题】:Getting System.InvalidOperationException: Request format is invalid: multipart/form-data error while upload image(data) via AFNetworking 【发布时间】:2013-04-09 08:07:33 【问题描述】:

我正在关注一些通过 AFNetworking 上传图像(数据)的教程。我已经完成了

 NSURL *url = [NSURL URLWithString:@"http://184.154.0.24"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

//    NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
//    [_params setObject:multiMData forKey:@"Bytes"];
//    [_params setObject:name forKey:@"UserId"];
//    [_params setObject:@"Hello" forKey:@"fileName"];
//    [_params setObject:@"png" forKey:@"contentType"];

//    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/IphoneVideoService/webservice.asmx/getStringData" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)

    [formData appendPartWithFileData:multiMData name:@"stringReturn" fileName:@"stringReturn.png" mimeType:@"image/png"];
];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
];


[operation setCompletionBlock:^
    NSLog(@"%@", operation.responseString); //Lets us know the result including failures
];

//    NSOperationQueue *queue = [[NSOperationQueue alloc] init] ;
//    [queue addOperation:operation];

[httpClient enqueueHTTPRequestOperation:operation];

编译后出现这个错误

2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 323584 of 333526 bytes
2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 327680 of 333526 bytes
2013-04-09 13:22:49.153 Softo Chat[582:19d03] Sent 331776 of 333526 bytes
2013-04-09 13:22:49.154 Softo Chat[582:19d03] Sent 333526 of 333526 bytes
2013-04-09 13:22:49.154 Softo Chat[582:1ad0b] System.InvalidOperationException: Request    format is invalid: multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

及其服务器端编程asp.net

[WebMethod]
public string getStringData(string stringReturn)

    return stringReturn;

【问题讨论】:

【参考方案1】:

嗨,Pravi jay,我只是在谷歌上搜索了一下,发现了一个与您相关的好问题。您只需要对 web.config 进行一些更改。

<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

请访问:-

'System.InvalidOperationException: Request format is invalid: multipart/form-data' error when posting image from iphone to .NET webservice

【讨论】:

以上是关于获取 System.InvalidOperationException:请求格式无效:通过 AFNetworking 上传图像(数据)时出现多部分/表单数据错误的主要内容,如果未能解决你的问题,请参考以下文章

js如何获取某一个元素,如果获取不到就继续获取,直到获取到后停止获取?

iOS ---------- 获取设备的各种信息

java反射获取属性值

Shell 获取路径

iOS 获取文件大小

根据日期字符串获取星期几,日期获取星期,时间获取星期,js获取星期