我在iPhone应用程序中访问xml Web服务。但我无法从服务器获取适当的数据。我在NSURLConnection的connectionDidFinishLoading委托方法中遇到以下错误。
皂:ServerServer 无法处理请求。 --->对象引用未设置为 对象的实例。
以下是我的代码:
NSString *deviceID = [OpenUDID value];
NSString *soapMsg = [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:Header>"
"<AuthHeader xmlns=\"http://tempuri.org/\">"
"<Username>admin</Username>"
"<Password>admin</Password>"
"</AuthHeader>"
"</soap:Header>"
"<soap:Body>"
"<WsGetAllDrugDetailsXML xmlns=\"http://tempuri.org/\">"
"<UserId>%d</UserId>"
"<IMEI>%@</IMEI>"
"</WsGetAllDrugDetailsXML>"
"</soap:Body>"
"</soap:Envelope>",[Appdelegate.UserID intValue],deviceID];
//---print it to the Debugger Console for verification---
NSLog(@"%@", soapMsg);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:Appdelegate.wsURL];
//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/WsGetAllDrugDetailsXML" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
我无法预测我身边出了什么问题。或者是服务器端的错误,我没有收到数据。 请帮助我。谢谢你!
答案 0 :(得分:0)
您的Content-Length
值不正确:
长度必须以字节为单位。 NSString的length
方法返回UTF-16代码单元的数量。
您可以按如下方式修复:
NSData* bodyData = [soapMsg dataUsingEncoding:NSUTF8StringEncoding];
[req addValue:[NSString stringWithFormat:@"%d",(int)[bodyData length]] forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:bodyData];