如何在 iphone sdk 中以字符串形式发送 xml?
我正在尝试调用一个包含 xml 参数的 Web 服务,但是当我运行代码时,不会进行 Web 服务调用,但是当我使用一些随机字符串作为参数时,就会调用 Web 服务。请告诉我出了什么问题用我的 xml 或者有其他方法来传递 xml。
NSString *xml=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><R><Root><CriteriaID>11</CriteriaID><Rating/></Root><Root><CriteriaID>10</CriteriaID><Rating/></Root><Root><CriteriaID>9</CriteriaID></Root><Root><CriteriaID>8</CriteriaID><Rating/></Root><Root><CriteriaID>7</CriteriaID><Rating/></Root><Root><CriteriaID>6</CriteriaID><Rating/></Root></R>"];
NSString *soapMsg = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<FullWomPrintsInsert xmlns=\"http://tempuri.org/\">\n"
"<xmlDoc>%@</xmlDoc>"
"</FullWomPrintsInsert>"
"</soap:Body></soap:Envelope>",xml];
NSLog(@"SoapMsg=%@",soapMsg);
NSString *club_url = [NSString stringWithFormat:@"http://abc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:club_url]];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
NSLog(@"Message Length..%@",msgLength);
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/FullWomPrintsInsert" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
I am trying to call a web servive in which there is xml paramater but when i run the code the web service call is not made but when i use some random string as a paramater then the web service gets called.Please tell me what is wrong with my xml or is there other way to pass xml.
NSString *xml=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><R><Root><CriteriaID>11</CriteriaID><Rating/></Root><Root><CriteriaID>10</CriteriaID><Rating/></Root><Root><CriteriaID>9</CriteriaID></Root><Root><CriteriaID>8</CriteriaID><Rating/></Root><Root><CriteriaID>7</CriteriaID><Rating/></Root><Root><CriteriaID>6</CriteriaID><Rating/></Root></R>"];
NSString *soapMsg = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<FullWomPrintsInsert xmlns=\"http://tempuri.org/\">\n"
"<xmlDoc>%@</xmlDoc>"
"</FullWomPrintsInsert>"
"</soap:Body></soap:Envelope>",xml];
NSLog(@"SoapMsg=%@",soapMsg);
NSString *club_url = [NSString stringWithFormat:@"http://abc"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:club_url]];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
NSLog(@"Message Length..%@",msgLength);
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/FullWomPrintsInsert" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 Mac(而不是 iPhone)上的同步连接将您的代码运行到我的服务器:
在 Charles 中捕获了正文:
这应该与您尝试发送的内容相匹配。
标题是:
如果您愿意,我可以在模拟器或设备上运行它。
I ran your code to my server with a sync connection on a Mac, not an iPhone:
Caught the body in Charles:
Which should match what you are trying to send.
The headers were:
If you want I can run it in the simulator or on a device.