从 Objective c 到 asmx Web 服务的参数为空白

发布于 2024-11-18 00:39:49 字数 2370 浏览 2 评论 0原文

我正在从我的 xcode 调试器调用 .Net asmx Web 服务。在调试器中,我可以看到我正在传递一些值,但在 .net Web 服务上它是空白的。

在网络服务端,我正在写入日志文件。我看到该方法正在被调用,但参数为空/空。我已经粘贴了下面的代码。我也粘贴了wsdl。参数作为空白

-(void) fetchData: (NSString*)userid 
{
    NSString *requestSoapMsg = 
    [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"
     "<UpdateRegForUser xmlns=\"http://soft.com\">\n"
     "<userid>%@</userid>\n"    
     "</UpdateRegForUser>\n"
     "</soap:Body>\n"
     "</soap:Envelope>\n", userid];

    NSLog(@"%@", requestSoapMsg); // i can see the value here

    NSURL *url = [NSURL URLWithString:@"http://soft.com:8000/services/registration.asmx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString *requestSoapMsgLength = [NSString stringWithFormat:@"%d", [requestSoapMsg length]];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://soft.com/UpdateRegForUser" forHTTPHeaderField:@"SOAPAction"];
    [request addValue:requestSoapMsgLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[requestSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSError *error;
    NSURLResponse *response;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(!result)
        NSLog(@"FAILEDDDDDDDDD!!");

WSDL 出现的任何原因 -

POST /services/registration.asmx HTTP/1.1

Host: soft.com


Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://soft.com/UpdateRegForUser"

<?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>
    <UpdateRegForUser xmlns="http://soft.com/">
      <userid>string</userid>
    </UpdateRegForUser>
  </soap:Body>
</soap:Envelope>

I am calling an .Net asmx web service from my xcode debugger. In the debugger I can see that I am passing some value but on the .net web service it is coming as blank.

On the web service end I am writing to a log file. I see that the method is getting called but the parameter is coming blank/null. I have pasted the code below. I have also pasted the wsdl. Any reason why the parameters are coming as blank

-(void) fetchData: (NSString*)userid 
{
    NSString *requestSoapMsg = 
    [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"
     "<UpdateRegForUser xmlns=\"http://soft.com\">\n"
     "<userid>%@</userid>\n"    
     "</UpdateRegForUser>\n"
     "</soap:Body>\n"
     "</soap:Envelope>\n", userid];

    NSLog(@"%@", requestSoapMsg); // i can see the value here

    NSURL *url = [NSURL URLWithString:@"http://soft.com:8000/services/registration.asmx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    NSString *requestSoapMsgLength = [NSString stringWithFormat:@"%d", [requestSoapMsg length]];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://soft.com/UpdateRegForUser" forHTTPHeaderField:@"SOAPAction"];
    [request addValue:requestSoapMsgLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[requestSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    NSError *error;
    NSURLResponse *response;

    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(!result)
        NSLog(@"FAILEDDDDDDDDD!!");

WSDL -

POST /services/registration.asmx HTTP/1.1

Host: soft.com


Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://soft.com/UpdateRegForUser"

<?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>
    <UpdateRegForUser xmlns="http://soft.com/">
      <userid>string</userid>
    </UpdateRegForUser>
  </soap:Body>
</soap:Envelope>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-11-25 00:39:49

我想到了问题所在。

我编写了一个 C# 控制台应用程序并调用了 Web 服务。同时,我捕获了 fiddler 的痕迹。 fiddler 跟踪显示 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 应该出现在 xsi 之前。我保存了 xml 并使我的请求 xml 完全相同。它开始工作了。

I figured the problem.

I wrote a C# console application and invoked the web service. At the same time, I captured the trace from fiddler. The fiddler trace showed me that xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" should come before xsi. I saved the xml and made my request xml EXACTLY the same. It started working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文