【问题标题】:Webmethod parameter stay nullWebmethod 参数保持为空
【发布时间】:2009-09-22 08:24:08
【问题描述】:

我正在开发一个 iPhone 应用程序,它将 SOAP 消息发送到 .NET Web 服务以调用多个 WebMethods。这些方法从数据库中获取数据并在 SOAP 消息中将其返回给 iPhone。

当我发送一个 SOAP 消息调用没有任何参数的 WebMethod 时,它工作得很好。

但是当我通过消息传递参数时,webMethod 参数在 Visual Studio 调试器中保持为空。看起来 WebMethod 没有收到任何东西。

我使用网络嗅探器查看传入的数据包,发现 SOAP 消息在进来时是正常的。所以一定是 web 服务端出了问题。

这里有一些代码:

网络方法:

public class ZDFMobielWebservice : System.Web.Services.WebService
{

    [WebMethod]
    public string getVerzekerde(string bsn)
    {          
        string result = bsn;
        ZDFKoppeling koppeling = new ZDFKoppeling();
        result = koppeling.getData(Convert.ToInt32(bsn));
        return result;
    }
}

创建 SOAP 消息的函数:

-(IBAction)buttonClick:(id)sender
{
  recordResults = FALSE;

  NSString *soapMessage = [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"
  "<getVerzekerde xmlns=\"http:/meijberg.topicus.local/zdfmobiel\">\n"
  "<bsn>%@</bsn>\n"
  "</getVerzekerde>\n"    
  "</soap:Body>\n"
  "</soap:Envelope>\n", bsnInput.text
  ];
  NSLog(soapMessage);

  NSURL *url = [NSURL URLWithString:@"http://meijberg.topicus.local/ZDFMobielWebservice.asmx"];
  NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

  [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  [theRequest addValue: @"http://meijberg.topicus.local/zdfmobiel/getVerzekerde" forHTTPHeaderField:@"SOAPAction"];
  [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
  [theRequest setHTTPMethod:@"POST"];
  [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

  NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

  if( theConnection )
  {
     webData = [[NSMutableData data] retain];
  }
  else
  {
     NSLog(@"theConnection is NULL");
  }

  //[nameInput resignFirstResponder];

}

这里是 SOAP 消息:

<?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>
      <getVerzekerde xmlns="http:/meijberg.topicus.local/zdfmobiel">
        <bsn>999999999</bsn>
      </getVerzekerde>
    </soap:Body>
  </soap:Envelope>

有谁知道问题出在哪里?

编辑:当我从网络服务器自动生成的网页调用网络方法时,它运行良好。然后用我输入的值填充参数。

【问题讨论】:

    标签: c# iphone xml soap


    【解决方案1】:

    经过多次网络搜索,我找到了答案。

    我使用工具 SoapUI (http://www.soapui.org/) 将一些肥皂消息发送到我的网络服务。 SoapUI 显示了它发送到 Web 服务的 XML 消息,所以我发现我在 iPhone 应用程序中的消息语法不正确。

    代替:

     NSString *soapMessage = [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"
     "<getVerzekerde xmlns=\"http:/meijberg.topicus.local/zdfmobiel\">\n"
     "<bsn>%@</bsn>\n"
     "</getVerzekerde>\n"    
     "</soap:Body>\n"
     "</soap:Envelope>\n", bsnInput.text
      ];
    

    一定是这样的:

        NSString *soapMessage = [NSString stringWithFormat:
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:zdf=\"http://meijberg.topicus.local/zdfmobiel\">\n"
    "<soapenv:Body>\n"
    "<zdf:getVerzekerde>\n"
    "<zdf:bsn>%@</zdf:bsn>\n"
    "</zdf:getVerzekerde>\n"
    "</soapenv:Body>\n"
    "</soapenv:Envelope>\n", bsnInput.text
    ];
    

    不过,我还是不明白为什么会这样。可能它与所有标签中的分号关键字有关。有人可以解释一下吗?

    【讨论】:

    • 我有同样的问题,但在我的场景中,调用者是一个创建soap xml并通过MSXML发送xml的windows应用程序,你认为在我的情况下使用上述关键字可以解决问题吗?
    【解决方案2】:

    在这一行:

    <getVerzekerde xmlns=\"http:/meijberg.topicus.local/zdfmobiel\">
    

    你需要在地址后面加一个斜杠(在zdfmobiel之后):

    <getVerzekerde xmlns=\"http:/meijberg.topicus.local/zdfmobiel/\">
    

    【讨论】:

    • 您能否将其发布为编辑,从而使答案变得完整且自给自足?
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2013-03-06
    • 2018-11-19
    • 2014-08-29
    • 2016-06-30
    • 2012-07-24
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多