【问题标题】:SOAP get content type error in AFNetworkingAFNetworking 中的 SOAP 获取内容类型错误
【发布时间】:2015-11-16 02:33:39
【问题描述】:

我正在使用 SOAP Web 服务并且遇到 Content-Type 错误,我尝试更改类型 "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}

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

【问题讨论】:

  • 这意味着服务器以 HTML 格式返回响应,通常是错误甚至堆栈跟踪。

标签: ios soap xml-parsing afnetworking


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 2016-12-06
    • 1970-01-01
    • 2017-08-15
    • 2012-11-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多