AFNetworking 2.0 Domain = AFNetworkingErrorDomain代码= -1011“请求失败:内部服务器错误(500)

时间:2014-03-13 11:52:33

标签: ios web-services afnetworking afnetworking-2

我正在尝试使用子类AFHTTPRequestOperationManager将我的代码转换为AFNetworking 2.0。这是我的代码

+ (NSAFNetwokingRequestManager *)sharedClient {
    static NSAFNetwokingRequestManager *_sharedClient = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:GET_CAR_BRAND]];
    });
    return _sharedClient;
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
     if (self) {
    self.responseSerializer = [AFXMLParserResponseSerializer serializer];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/soap+xml"];
    self.requestSerializer = [AFHTTPRequestSerializer serializer];
    [self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"];
}

    return self;
}
- (void)requestBrandcompletion:(NSAFNetwokingRequestManagerCompletionBlock)completion {
    NSString *soapRequest=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
    "<soap:Body>\n"
    " <CarBrandExt xmlns=\"http://www.nohausystems.nl/\" />\n"
    "</soap:Body>\n"
    "</soap:Envelope>\n";
    NSString *msgLength = [NSString stringWithFormat:@"%i",[soapRequest length]];
    [self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (completion) {
            completion(YES, responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (completion) {
            completion(NO, nil);
            NSLog(@"Unable to fetch record error %@ with user info %@.", error, error.userInfo);
        }
    }];
}

我收到此错误Domain = AFNetworkingErrorDomain Code = -1011&#34;请求失败:内部服务器错误(500)。任何人都可以告诉我这里我做错了什么吗? 得到这个回应:

{ status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 509;
    "Content-Type" = "application/soap+xml; charset=utf-8";
    Date = "Thu, 13 Mar 2014 12:59:45 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
} }

4 个答案:

答案 0 :(得分:2)

在您的代码中,您有:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];

将内容类型定义为&#34; text / xml&#34;,而服务器显然期待&#34; application / soap + xml&#34;。您应该尝试将代码的这一部分更改为:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
          [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/sopa+xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]];

更新了建议:

尝试添加:

[self.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

- (instancetype)initWithBaseURL:(NSURL *)url方法的最后。

如果这没有帮助,我建议做一些更详细的网络请求调试。你可以,例如设置AFNetworkActivityLogger以将请求/响应信息记录到控制台。

答案 1 :(得分:1)

BASEURLAFNetworking一起使用时,请小心谨慎。如果您要向AFNetworking提供确切的端点网址,则可能会失败。

例如:

您有一个端点 URL = http://myweb.com/api.php

在上述情况下,如果您使用的是AFNetworking,则 BASEURL != URL ,而它应该类似于BASEURL=http://myweb.com/并且在调用任何请求时 URL = api.php 即可。 AFNetworking附加 BASEURL +网址,并创建一个完整的端点网址。

虽然上述答案可能不适合您的情况。但是如果它有助于驯服其他问题,或者也可能在你的情况下。

感谢。

答案 2 :(得分:1)

&#34;请求失败:不可接受的内容类型:application / soap + xml&#34;

生成错误
- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                    data:(NSData *)data
                   error:(NSError * __autoreleasing *)error

AFHTTPResponseSerializer的方法,如果出现无法接受的MIME类型的响应。

您可以使用以下代码修复它

self.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSSet *set = self.responseSerializer.acceptableContentTypes;
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"application/soap+xml"];

或修改AFXMLDocumentResponseSerializer - 添加&#34; application / soap + xml&#34;可接受的内容类型:

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml", @"application/soap+xml", nil];

    return self;
}

以下不会解决这个问题我想

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"];

没有足够的信息可以理解为什么会这样做&#34;&#34;请求失败:内部服务器错误(500)。&#34;我会发布正确和错误的请求,我们会尽力帮助您。

答案 3 :(得分:1)

试试这段代码:

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Accept"];