【问题标题】:making a soap request in iphone在 iphone 中发出肥皂请求
【发布时间】:2014-06-27 17:19:42
【问题描述】:

我正在尝试使用仅返回日期(测试)的肥皂网络服务。但我无法连接到网络服务。我只从 Web 服务接收 wsdl,但无法获得所需的数据。以下是我在目标 c 中的代码

 NSString *soapMessage = @"<?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"
"<getDate xmlns:type=\"xsd:string\">\n"
"</getDate>\n"
"</soap:Body>\n"
"</soap:Envelope>\n";
NSURL *url = [NSURL URLWithString:@"http://10.1.6.5/gnosis2/public/wslogin/"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[req addValue:@"text/xml; charset=utf-8"  forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://10.1.6.5/gnosis2/public/wslogin/" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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

我想使用网络服务中的 getDate 方法。有人可以帮我吗?

编辑
上面的代码给了我以下与浏览器中查看的 wsdl 相同的 xml:

<?xml version="1.0"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://10.1.6.5/gnosis2/public/wslogin/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="MyWebService" targetNamespace="http://10.1.6.5/gnosis2/public/wslogin/"><types><xsd:schema targetNamespace="http://10.1.6.5/gnosis2/public/wslogin/"/></types><portType name="MyWebServicePort"><operation name="getDate"><documentation>Get the server date and time</documentation><input message="tns:getDateIn"/><output message="tns:getDateOut"/></operation><operation name="getAgeString"><documentation>Get a nicely formatted string of a person's age</documentation><input message="tns:getAgeStringIn"/><output message="tns:getAgeStringOut"/></operation></portType><binding name="MyWebServiceBinding" type="tns:MyWebServicePort"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="getDate"><soap:operation soapAction="http://10.1.6.5/gnosis2/public/wslogin/#getDate"/><input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></input><output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></output></operation><operation name="getAgeString"><soap:operation soapAction="http://10.1.6.5/gnosis2/public/wslogin/#getAgeString"/><input><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></input><output><soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://10.1.6.5/gnosis2/public/wslogin/"/></output></operation></binding><service name="MyWebServiceService"><port name="MyWebServicePort" binding="tns:MyWebServiceBinding"><soap:address location="http://10.1.6.5/gnosis2/public/wslogin/"/></port></service><message name="getDateIn"/><message name="getDateOut"><part name="return" type="xsd:string"/></message><message name="getAgeStringIn"><part name="name" type="xsd:string"/><part name="age" type="xsd:int"/></message><message name="getAgeStringOut"><part name="return" type="xsd:string"/></message></definitions>

谢谢
潘卡伊

【问题讨论】:

  • 感谢您发布soap消息字符串格式,它只是解决了我格式化soap请求的问题:)

标签: iphone soap


【解决方案1】:

【讨论】:

  • 我已经尝试使用这些文章,几乎完成了这里提到的所有内容,但仍然无法正常工作
  • @pankaj,你正好卡在那里。我的朋友,我没有正确回答您的问题。
  • 我正在从服务器返回完整的 wsdl,但我没有从服务器获得所需的响应。
  • 我已经粘贴了我收到的 xml 作为响应
  • 我认为您没有正确编写 SOAP 请求。
【解决方案2】:

非常简单的方法是生成 WSDL 类。 可以使用https://code.google.com/p/wsdl2objc/ 做到这一点。 听从https://code.google.com/p/wsdl2objc/wiki/UsageInstructions的指示

【讨论】:

    【解决方案3】:

    以下代码块帮助我使用 SOAP 请求处理 Web 服务。

    -(void)getWebServiceContent {
    
        NSURL *url = [NSURL URLWithString:@"http://url_value_here"];
        NSString *soapBody = @"soap_body_here";
    
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPMethod:@"POST"]; //GET
        [request addValue:@"Value_to_be_added" forHTTPHeaderField:@"SOAPAction"];
        [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject{
    
            NSLog(@"Success : Content : %@",[operation responseString]);
        } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
    
            NSLog(@"Failed");
        })];
    
        [operation start];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多