请求格式无效:text/xml; charset = utf-8
我是iphone开发新手。请求格式无效:text/xml; charset = utf-8
我想从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:请求格式无效:文本/ XML;字符集= UTF-8。 在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
如何在来到这个问题?
不要使用NSURLConnection的只是设置肥皂envelope..use这个代替
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]);
}
以为我又得到了同样的错误。我已经在safari上检查了我的网址。它给出了正确的答复。但我的代码不能。 –
你在哪里使用了你的文件中的上述代码? – AppleDelegate
在我的应用程序中的UIViewController.m文件中 –
显示代码用于接收数据.. – AppleDelegate
后 - (无效)连接:(NSURLConnection的*)连接didReceiveResponse:(NSURLResponse * )响应 {} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {NSMutableData * webdata = [[NSMutableData alloc] initWithData:data]; NSString * theXML = [[NSString alloc] initWithBytes:[webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding]; NSLog(@“received theXML:%@”,theXML); } - (无效)连接:(NSURLConnection的*)连接didFailWithError:(NSError *)错误 {的NSLog(@ “用theConenction ERROR”);} - (无效)connectionDidFinishLoading:(NSURLConnection的*)连接 {} –