【问题标题】:AFNetworking 2.0 Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)AFNetworking 2.0 域=AFNetworkingErrorDomain 代码=-1011 “请求失败:内部服务器错误 (500)
【发布时间】:2014-04-18 03:17:45
【问题描述】:

我正在尝试使用子类 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 "请求失败:内部服务器错误 (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";
} }

【问题讨论】:

  • 不查看您的代码,错误 500 是服务器错误,您查看服务器的日志了吗?
  • 相同的 Web 服务在旧版本的 AFNetworking 上运行良好
  • 所以我想我的实现有一些问题。
  • 为 requestserializer [self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"]; 设置内容类型怎么样?
  • 另外我建议安装一个 web 调试代理,如 fiddler 或 charles 来检查 web 流量。然后,您可以轻松地比较工作请求和非工作请求。

标签: ios web-services afnetworking afnetworking-2


【解决方案1】:

在您的代码中:

[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]];

将内容类型定义为“text/xml”,而服务器显然期待“application/soap+xml”。您应该尝试将那部分代码更改为:

[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 将请求/响应信息记录到控制台。

【讨论】:

  • 我对它进行了更详细的研究(我对 AFNetworking 2.0 也很陌生)并更新了答案。如果可行,请尝试。
【解决方案2】:

只是一个想法,在使用 BASEURLAFNetworking 时要小心。如果您向 AFNetworking 提供确切的端点 url,它可能会失败。

例如:

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

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

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

谢谢。

【讨论】:

    【解决方案3】:

    “请求失败:不可接受的内容类型:应用程序/soap+xml”错误是由

    生成的
    - (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 - 将“application/soap+xml”添加到可接受的内容类型:

    - (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"];
    

    没有足够的信息来理解您为什么会出现“”请求失败:内部服务器错误 (500)。我将发布正确和不正确的请求,我们将尽力帮助您。

    【讨论】:

    • 我认为[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"]; 无法解决您的任何问题。我已经更新了答案,请检查。
    • 谢谢 这解决了我的应用程序/soap+xml 问题,但仍然出现内部服务器错误..
    【解决方案4】:

    试试这个代码:

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

    【讨论】:

    • 我有同样的问题,当我应用你的解决方案时,我的项目通过给出“这个类不符合键'Accept'的键值编码”而崩溃。您对此有任何想法/解决方案吗?提前致谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    相关资源
    最近更新 更多