SOAP 在 AFNetworking 中获取内容类型错误
SOAP get content type error in AFNetworking
我正在使用 SOAP Web 服务,但出现内容类型错误,我尝试更改类型 "application/soap+xml; charset=utf-8"、"text/xml; charset=utf-8",但出现了同样的错误。
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<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/\">"
"<soap:Body>"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
"<Celsius>140.0</Celsius>"
"</CelsiusToFahrenheit>"
"</soap:Body>"
"</soap:Envelope>"];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml" forHTTPHeaderField:@"Content-Type"];//
[theRequest addValue: @"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//parse NSXMLParser object here if request successfull
if ([responseObject isKindOfClass:[NSXMLParser class]]) {
NSXMLParser *parser = (NSXMLParser *)responseObject;
NSError *error;
NSDictionary *dict = [XMLReader dictionaryForXMLData:parser error:&error];
NSLog(@"JSON: %@ : %@", responseObject,dict);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:operation];
然后出现以下错误...
NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
非常感谢您的帮助和建议。谢谢
您使用的 URL 与 example 中的不同。如果您像这样更改代码,它将起作用。
// take a look on www, it is important
NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
我正在使用 SOAP Web 服务,但出现内容类型错误,我尝试更改类型 "application/soap+xml; charset=utf-8"、"text/xml; charset=utf-8",但出现了同样的错误。
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<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/\">"
"<soap:Body>"
"<CelsiusToFahrenheit xmlns=\"http://www.w3schools.com/webservices/\">"
"<Celsius>140.0</Celsius>"
"</CelsiusToFahrenheit>"
"</soap:Body>"
"</soap:Envelope>"];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml" forHTTPHeaderField:@"Content-Type"];//
[theRequest addValue: @"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//parse NSXMLParser object here if request successfull
if ([responseObject isKindOfClass:[NSXMLParser class]]) {
NSXMLParser *parser = (NSXMLParser *)responseObject;
NSError *error;
NSDictionary *dict = [XMLReader dictionaryForXMLData:parser error:&error];
NSLog(@"JSON: %@ : %@", responseObject,dict);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:operation];
然后出现以下错误...
NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
非常感谢您的帮助和建议。谢谢
您使用的 URL 与 example 中的不同。如果您像这样更改代码,它将起作用。
// take a look on www, it is important
NSURL *url = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.w3schools.com/webservices/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];