SOAP在AFNetworking中获取内容类型错误

时间:2015-08-22 04:29:20

标签: ios soap xml-parsing afnetworking

我正在使用SOAP Web服务,我收到Content-Type错误,我尝试更改类型" application / soap + xml; charset = utf-8"," text / xml;字符集= 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}

感谢您的帮助和建议。谢谢

1 个答案:

答案 0 :(得分:0)

您使用的网址与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"];