调用WebService时参数不起作用

时间:2013-06-23 16:17:56

标签: c# asp.net ios objective-c web-services

我创建了一个带有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;
    }

我在客观方面称呼它,就像以下方式一样:

    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];

将创建并发送以下soap-message:

<?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。我做错了什么?

0 个答案:

没有答案