【问题标题】:Send parameters to a web service将参数发送到 Web 服务
【发布时间】:2011-02-04 13:52:39
【问题描述】:

开始之前:我正在使用 Objective C 为 Iphone 编程。

我已经使用 NSURLRequest 和 NSURLConnection 实现了对 Web 服务函数的调用。然后该函数返回一个包含我需要的信息的 XML。

代码如下:

NSURL *url = [NSURL URLWithString:@"http://myWebService/function"];
NSMutableURLRequest theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

我也实现了方法

  • didRecieveResponse
  • didRecieveAuthenticationChallenge
  • didRecievedData
  • didFailWithError
  • connectionDidFinishLoading.

而且效果很好。

现在我需要向函数发送 2 个参数:“位置”和“模块”。
我尝试使用以下修改:

NSMutableURLRequest theRequest   = [[NSMutableURLRequest alloc] initWithURL:url];
[theRequest setValue:@"USA" forHTTPHeaderField:@"location"];
[theRequest setValue:@"DEVELOPMENT" forHTTPHeaderField:@"module"];
NSURLConnection theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

但它似乎不起作用。 我做错了什么?有没有办法知道我是否使用了错误的参数名称(可能是“位置”或“位置”,或者没关系?)? 或者知道函数在等待哪些参数的方法...

额外信息:
我无权访问 Web 服务的源代码,因此无法对其进行修改。 但我可以访问 WSDL。制作函数的人都在那里......但我无法理解它>.<......>

任何帮助将不胜感激。 :)

【问题讨论】:

  • 是的!我认为 headerFields 不是这样做的地方......但我很绝望 XD
  • 如果您可以访问 WSDL,您可能需要查看 wsdl2objc:code.google.com/p/wsdl2objc

标签: iphone objective-c web-services parameters


【解决方案1】:

如果涉及 WSDL,它可能是一个 SOAP Web 服务,因此它可能需要一些 SOAPy XML 作为输入,我认为您不会这样做。不妨看看这个问题:How to access SOAP services from iPhone

【讨论】:

    【解决方案2】:

    您在 HTTP 标头中设置值,而不是 HTTP 请求中的 GET 或 POST 参数。这可能不是你想要做的。

    如果服务器接受 GET 请求中的参数,您可以在 URL 中执行类似的操作:

    "http://myWebService/function?location=USA&module=DEVELOPMENT"
    

    否则,您将不得不按照 MK 所说的那样走完整的 SOAP 路线。

    【讨论】:

    • @Alejandra 注意:我刚刚更正了 URL 字符串中的错字:应该是“?”在参数之前,而不是“;”和我一样
    【解决方案3】:

    一些关于 GET Post SOAP 的例子

    GET 请求

    GET /index.html?userid=joe&password=guessme HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    

    发布请求

    POST /login.jsp HTTP/1.1
    Host: www.mysite.com
    User-Agent: Mozilla/4.0
    Content-Length: 27
    Content-Type: application/x-www-form-urlencoded
    
    userid=joe&password=guessme
    

    肥皂请求

    POST /InStock HTTP/1.1
    Host: www.example.org
    Content-Type: application/soap+xml; charset=utf-8
    Content-Length: nnn
    
    <?xml version="1.0"?>
    <soap:Envelope
    xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
    soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
    
    <soap:Body xmlns:m="http://www.example.org/stock">
      <m:GetStockPrice>
        <m:StockName>IBM</m:StockName>
      </m:GetStockPrice>
    </soap:Body>
    
    </soap:Envelope>
    

    HOST、User-Agent、Content-Length、Content-Type是Request Header中的项目

    【讨论】:

      【解决方案4】:

      我花了几个小时才完成这项工作,所以我想我会发布我的解决方案,以便将来为其他人节省一些时间。这就是我如何让 Objective-c 从带有字符串参数的 .NET WCF 服务中检索 JSON 数据。

      (我使用 wireshark 捕获了一个与 wcf 服务对话的 .net 客户端,因此我知道我需要从我的 iOS 客户端传递什么)

      static NSString *feedURLString = @"http://www.mydomain.com/DownloadService.svc";
      NSMutableURLRequest *faURLRequest =
      [NSMutableURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
      
      [faURLRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
      [faURLRequest setHTTPMethod:@"POST"]; 
      [faURLRequest addValue:@"http://tempuri.org/IDownloadService/GetChanges" forHTTPHeaderField:@"SOAPAction"];
      
      NSString *localLastSync;
      if (userProfile.lastSync == nil)
      {
          localLastSync = @"2011-09-01T12:00:00";
      }
      else
      {
          localLastSync = userProfile.lastSync;
      }
      
      NSString *soapMessage = [NSString stringWithFormat:
                               @"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                               "<s:Body >\n"
                               "<GetChanges xmlns=\"http://tempuri.org/\">\n"
                               "<lastSync>%@</lastSync>\n"
                               "</GetChanges>\n"
                               "</s:Body>\n"
                               "</s:Envelope>\n", localLastSync];
      [faURLRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
      
      self.downloadConnection =
      [[NSURLConnection alloc] initWithRequest:faURLRequest delegate:self];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多