获取System.InvalidOperationException:请求格式无效:通过AFNetworking上传图像(数据)时出现multipart / form-data错误

时间:2013-04-09 08:07:33

标签: iphone .net ios web-services afnetworking

我正在关注一些通过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 个答案:

答案 0 :(得分:2)

嗨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