如何连接SOAP Web服务?

时间:2010-09-09 14:01:21

标签: iphone web-services soap wsdl

我正在使用从iPhone连接到SOAP webservice。我无法连接到Web服务。在这里,我收录了我的代码:

        NSString *soapMessage = [NSString stringWithFormat:
        @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
        "<SOAP-ENV:Envelope \n"
        "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
        "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
        "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
        "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
        "xmlns:mob=\"http://XXX.edu\">\n"
        "<SOAP-ENV:Body> \n"
        "<mob:Login>\n"
        "<mob:username>User Name</mob:username>\n"
        "<mob:password>Password</mob:password>\n"
        "</mob:Login>\n"
        "</SOAP-ENV:Body> \n"
        "</SOAP-ENV:Envelope>"];



        NSURL *url = [NSURL URLWithString:@"https://YYY.edu/UserService.svc"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

        [request addValue: @"Login" forHTTPHeaderField:@"SOAPAction"];

        [request addValue: @"text/xml; charset=\"utf-8" forHTTPHeaderField:@"Content-Type"];

        [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];

        [request setValue:@"https://YYY/UserService" forHTTPHeaderField:@"Host"];

        [request setHTTPMethod:@"POST"];

        [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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


        WSDL URL  : https://XXX/UserService.svc?wsdl

        End point : https://XXX/UserService.svc


        **WSDL XML format**

        <wsdl:definitions name="UserService" targetNamespace="http://tempuri.org/">
        <wsdl:import namespace="http://YYY/" location="https://XXX/UserService.svc/basic?wsdl=wsdl1"/>
        <wsdl:types/>
        <wsdl:service name="UserService">
        <wsdl:port name="ZZZ" binding="i0:ZZZ">
        <soap:address location="https://XXX/UserService.svc"/>
        </wsdl:port>
        </wsdl:service>
        </wsdl:definitions>


        This is the XMl format and request input i had used. I cant connect with the    web service. I got the error message (response) is either empty or "<h1>Bad Request (Invalid Hostname)</h1>".

       where i am getting lack. Please any one help me.

       Thanks in advance.

1 个答案:

答案 0 :(得分:0)

尝试更改主机,在webservices中,您可以检查主机标头以与发布Web服务的主机相匹配。可能将标题“Host:”参数设置为正确的值可以解决问题。在头参数中只放置host:port。