我创建了一个带有闲置 WebMethod 的 .NET-WebService:
[WebMethod]
public DataSet getMyDataset(string param1, string param2) {
var ret = new DataSet();
var dt = new DataTable();
dt.Columns.Add("pk", typeof (string));
dt.Rows.Add("aaa");
dt.Rows.Add("bbb");
dt.Rows.Add("ccc");
ret.Tables.Add(dt);
return ret;
}
我在objective-c中调用它,就像休闲方式一样:
NSMutableString *sRequest = [[NSMutableString alloc] init];
self.SOAPActionURL = [NSString stringWithFormat:@"%@%@",self.XMLNameSpace, self.MethodName];
//make soap request
[sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
[sRequest appendString:@"<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/\">"];
[sRequest appendString:@"<soap:Body>"];
[sRequest appendString:[NSString stringWithFormat:@"<%@ xmlns=\"%@\">",MethodName, XMLNameSpace]];
if(MethodParametersAsString != nil) [sRequest appendString:MethodParametersAsString];
NSEnumerator *tableIterator = [MethodParameters keyEnumerator];
NSString *keyID;
while(keyID = [tableIterator nextObject])
{
[sRequest appendString:[NSString stringWithFormat:@"<%@>%@</%@>", keyID, [MethodParameters objectForKey:keyID], keyID]];
NSLog(@"Method: %@ Parameter:%@ Value:%@",self.MethodName, keyID, [MethodParameters objectForKey:keyID], [NSString stringWithFormat:@"<%@>%@</%@>", keyID, [MethodParameters objectForKey:keyID], keyID]);
}
//close envelope
[sRequest appendString:[NSString stringWithFormat:@"</%@>", MethodName]];
[sRequest appendString:@"</soap:Body>"];
[sRequest appendString:@"</soap:Envelope>"];
NSLog(sRequest);
NSURL *myWebserverURL = [NSURL URLWithString:XMLURLAddress];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:self.SOAPActionURL forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
NSData *envelope = [sRequest dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:envelope];
[request setValue:[NSString stringWithFormat:@"%d", [envelope length]] forHTTPHeaderField:@"Content-Length"]; //[sRequest
NSError *WSerror = nil;
NSURLResponse *WSresponse = nil;
NSMutableData *myMutableData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
这将创建并发送以下肥皂消息:
<?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:Body>
<getMyDataset xmlns="http://symas-design.ch">
<param1>val1</param1>
<param2>val2</param2>
</getMyDataset>
</soap:Body>
</soap:Envelope>
现在我的问题:
WebMethod getMyDataset 被正确调用 - 但参数 param1 和 param2 始终为 null(而不是此示例中的 val1 和 val2)。getMyDataset 的返回值也正确传输回 iPhone。只有参数不会进入 getMyDataset。我做错了什么?