【问题标题】:How to pass parameters in SOAP web service having a Method?如何在具有方法的 SOAP Web 服务中传递参数?
【发布时间】:2016-09-02 04:27:56
【问题描述】:

我从未使用过基于 SOAP 的服务,我不确定是否使用以下 SOAP 服务。

http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx?op=EmployeesLoginMethod

在此服务中,我需要使用参数传递 4 个值: 用户名、密码、IP 地址和设备名。 并且输出为 JSON 格式。

请帮助达到预期的结果。

【问题讨论】:

标签: ios objective-c soap


【解决方案1】:

您只需要一个 HTTP 发布请求并传递正确的 XML 数据并设置 HTTP 标头:

func soapRequest(username:String, password:String, ipAddress:String, deviceName:String){
        if let url = NSURL.init(string: "http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx"){
            let request = NSMutableURLRequest(URL: url)
            let requestBody = "<?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><EmployeesLoginMethod xmlns=\"http://tempuri.org/\"><username>\(username)</username><password>\(password)</password><IpAddress>\(ipAddress)</IpAddress><deviceName>\(deviceName)</deviceName></EmployeesLoginMethod></soap:Body></soap:Envelope>"
            request.HTTPMethod = "POST"
            request.HTTPBody = requestBody.dataUsingEncoding(NSUTF8StringEncoding)
            request.setValue("text/xml", forHTTPHeaderField: "Content-Type")
            request.setValue("\"http://tempuri.org/EmployeesLoginMethod\"", forHTTPHeaderField: "SOAPAction")
            NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in
                guard error == nil && data != nil else{
                    //handle error
                    return
                }
                if let responseString = String.init(data: data!, encoding: NSUTF8StringEncoding){
                    print("\(responseString)")
                }
                //..........
                //Parse your XML response data
                //.........
            }).resume()
            
        }
        
    }

【讨论】:

  • 但在 android 中,他们以 Json 格式获得响应,而我以 Xml 格式获得。
  • 根据您提供的 API 链接,响应将采用 XML 格式。尽管某处可能有可用的 JSON api。
  • 哪里需要改变那个 api
  • 你能帮帮我吗。
  • @DineshGurrapu 为您提供 API 的人可能有 Json 版本。或者您可以搜索将 XML 转换为 json,有库可以帮助您做到这一点。
【解决方案2】:

希望对你有帮助

这是在 Objective-C 中调用 SOAP 的方法:

NSURL *baseURL = [NSURL URLWithString:@"http://yourbaseURLxxxxxxxx"];

    NSString *sSOAPMessage = [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"
                              "<EmployeesLoginMethod xmlns=\"http://tempuri.org/\">"
                              "<username>iamiosguy@gmail.com</username>"
                              "<password>lovetocode</password>"
                              "<ipaddress>192.16.0.0</ipaddress>"
                              "<devicename>iPhone7</devicename>"
                              "</EmployeesLoginMethod>"
                              "</soap:Body>\n"
                              "</soap:Envelope>\n"];

    NSLog(@"-----Developed Envelope----%@",sSOAPMessage);



    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[sSOAPMessage dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:@"http://tempuri.org/IService/EmployeesLoginMethod" forHTTPHeaderField:@"SOAPAction"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Data string = %@",string);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
    }];

    [operation start];

【讨论】:

    【解决方案3】:

    根据我在服务定义中看到的内容,输出也将是肥皂。

    我假设您想使用 c# 从另一台服务器调用该函数,截至撰写本文时,没有任何关于所使用技术的指示。您需要添加 wcf 引用并使用生成的类调用方法。

    在当前解决方案中添加对服务的引用 在解决方案资源管理器中,右键单击要将服务添加到的项目的名称,然后单击“添加服务引用”。 出现添加服务引用对话框。 单击发现。 当前解决方案中的所有服务(WCF 数据服务和 WCF 服务)都添加到服务列表中。 在服务列表中,展开您要使用的服务的节点并选择一个实体集。 在命名空间框中,输入要用于引用的命名空间。 单击确定以添加对项目的引用。 生成服务客户端(代理),并将描述服务的元数据添加到 app.config 文件中。

    MSDN How to: Add, Update, or Remove a WCF Data Service Reference


    如果您从 javascript 发布,这里是一个 vanilla js 示例。

    var soap_post_body = ""+
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+ 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
    '  <soap12:Body>'+
    '    <EmployeesLoginMethod xmlns="http://tempuri.org/">'+
    '      <username>string</username>'+
    '      <password>string</password>'+
    '      <IpAddress>string</IpAddress>'+
    '      <deviceName>string</deviceName>'+
    '    </EmployeesLoginMethod>'+
    '  </soap12:Body>'+
    '</soap12:Envelope>';
    
    xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx');
    xhr.setRequestHeader('Content-Type', 'application/soap+xml; charset=utf-8');
    xhr.onload = function() {
        if (xhr.status === 200) {
            console.log(xhr.responseText);
        }
        else {
            console.log(xhr.status);
        }
    };
    xhr.send(encodeURI(soap_post_body));

    【讨论】:

    • { URL: workforce.wifisocial.in/WebServicesMethods/… } { 状态码: 500, headers { "Cache-Control" = private; “内容长度”= 509; “内容类型”=“应用程序/soap+xml;字符集=utf-8”;日期 =“格林威治标准时间 2016 年 9 月 2 日星期五 05:17:34”;服务器 = "Microsoft-IIS/8.0"; “X-AspNet-版本”=“4.0.30319”; "X-Powered-By" = "ASP.NET"; "X-Powered-By-Plesk" = PleskWin; } }
    • 回复是这样的
    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多