请求格式无效:text / xml;字符集= utf-8的

时间:2012-09-27 08:02:45

标签: iphone ipad nsurlconnection nsurl nsurlconnectiondelegate

我是iphone开发的新手。

我正在尝试从iphone应用程序向服务器发送NSSTRING。

使用以下代码行

NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                             "<soap12:Body>\n"
                             "<SaveData xmlns=\"http://tempuri.org/\">\n"
                             "<xmlstring>%@</xmlstring>\n"
                             "</SaveData>\n"
                             "</soap12:Body>\n"
                             "</soap12:Envelope>\n", StringXml
                             ];
    NSLog(@"Soap Messaage......%@",soapMessage);

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://tempuri.org/SaveData" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
   // [theRequest addValue: StringXml forHTTPHeaderField:@"xmlstring"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    //[theRequest setHTTPBody: [StringXml dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

但它回复了我错误的回复

即。

System.InvalidOperationException:请求格式无效:text / xml;字符集= UTF-8。    在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()    在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在设置肥皂信封后不要使用NSURLConnection。请改用

NSError *WSerror;
        NSURLResponse *WSresponse;

        @try
        {

        NSMutableData *resMutableData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&WSresponse error:&WSerror];


            NSString *theResponse = [[NSString alloc]initWithBytes:[resMutableData mutableBytes] length:[resMutableData length] encoding:NSUTF8StringEncoding];
            NSLog(theResponse);

        }
        @catch( NSException* ex )
        {
            NSLog(@"Webservice Request Failed: Error %@", [WSerror code]);
        }