我正在尝试从我的xcode项目连接到web服务。我的设置适用于调用方法和gettin返回值,但不是当我尝试将参数传递给服务时。继承我的代码:
NSString *soapMsg = @"<?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/\"><soap:Body\n>
<TestArgMethod xmlns=\"http://tempuri.org\">\n
<argument>argument1</argument>
</TestArgMethod>
</soap:Body>
</soap:Envelope>";
NSURL *url = [NSURL URLWithString:@"http://myserver/myservice.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/TestArgMethod" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
NSLog(soapMsg);
if (conn) {
webData = [[NSMutableData data] retain];
}
用C#编写的服务是这样的:
[Webmethod]
public string TestArgMethod(string argument) {
return argument;
}
这是我缺少的东西吗?
<argument>argument1</argument>
?该服务无法识别发送参数,只识别该方法被调用。传递这些参数时,是否需要对它们进行不同的编码以表明它是一个字符串?
答案 0 :(得分:0)
你的请求一定是个问题。我正在使用你的字符串我得到很多错误。像这样格式化你的xml值并检查NSLog。
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<query>\n"
"<Key>%@</Key>\n"
"<Email>%@</Email>\n"
"</query>\n",key,email];
NSLog(@"%@",soapMessage);