Iphone 请求格式无效:text/xml;字符集=utf-8
我是iphone开发新手 我正在尝试从iphone应用程序向服务器发送NSSTRING 使用以下代码行Iphone 请求格式无效:text/xml;字符集=utf-8,iphone,ipad,nsurlconnection,nsurl,nsurlconnectiondelegate,Iphone,Ipad,Nsurlconnection,Nsurl,Nsurlconnectiondelegate,我是iphone开发新手 我正在尝试从iphone应用程序向服务器发送NSSTRING 使用以下代码行 NSString *soapMessage = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<soap12:Envelope xmlns:xsi=\"h
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];
NSString*soapMessage=[NSString stringWithFormat:
@“\n”
“\n”
“\n”
“\n”
“%@\n”
“\n”
“\n”
“\n”,字符串XML
];
NSLog(@“Soap消息……”%@,soapMessage);
NSURL*url=[NSURL URLWithString:urlString];
NSMutableURLRequest*theRequest=[NSMutableUrlRequestWithURL:url];
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMessage length]];
[theRequest addValue:@“text/xml;charset=utf-8”用于HttpHeaderField:@“Content Type”];
[请求附加值:@”http://tempuri.org/SaveDataforHTTPHeaderField:@“SOAPAction”];
[theRequest addValue:msgLength for HttpHeaderField:@“内容长度”];
//[TheRequestAddValue:StringXml for HttpHeaderField:@“xmlstring”];
[TheRequestSetHttpMethod:@“POST”];
[TheRequestSetHttpBody:[soapMessage数据使用编码:NSUTF8StringEncoding];
//[TheRequestSetHttpBody:[StringXml数据使用编码:NSUTF8StringEncoding];
NSURLConnection*connectionResponse=[[NSURLConnection alloc]initWithRequest:theRequest委托:self];
但它给了我错误的回答
i、 e
System.InvalidOperationException:请求格式无效:text/xml;字符集=utf-8。
在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()中
在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()中
如何解决此问题?不要在设置soap信封后立即使用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]);
}
显示接收数据的代码..-(void)连接:(NSURLConnection*)连接didReceiveResponse:(NSURLResponse*)响应{}-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{NSMutableData*webdata=[[NSMutableData alloc]initWithData:data];NSString*XML=[[NSString alloc]initWithBytes:[webdata mutableBytes]长度:[webdata length]编码:NSUTF8StringEncoding];NSLog(@“接收到XML:%@”,theXML);}-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误{NSLog(@“带有组件的错误”);}-(无效)连接完成加载:(NSURLConnection*)连接{}认为我又遇到了同样的错误。我已经在safari上查看了我的url。它给出了正确的答复。但我的代码无法在我的应用程序中的UIViewController.m文件中找到