【发布时间】: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