如何让iOS与.NET Web服务交流

时间:2012-07-12 15:37:11

标签: iphone objective-c

我在连接到这个网络服务时遇到问题,有什么想法我做错了吗?

- (void)startRetrievingHotels 
{    
    //Create the web service URL
    NSURL *url = [NSURL URLWithString:@"https://www.stratexlive.com/tenants/commercialbank/_vti_bin/stratexws.asmx"];

    //Create the web service request
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    NSString *requestString = [self buildHotelListRequest];
    NSLog(@"%@", requestString );

    [request setDelegate:self]; 
    [request setUsername:@"asdfasdf@asdfasdf.com"];
    [request setPassword:@"asdfasdfasdfasdf$"];
    [request appendPostData:[[self buildHotelListRequest] dataUsingEncoding:NSUTF8StringEncoding]]; //Set the xml request
    [request startAsynchronous];
}

- (NSString *)buildHotelListRequest
{


    NSString *result = [NSString stringWithFormat: @"<?xml version=\"1.0\"?>"
    @"<GetUserName xmlns=\"https://www.sdf.com/\" />"];

    NSLog(@"HOTEL LIST REQUEST: %@", result);

    return result;
}

这是我得到的回复:

> <?xml version="1.0" encoding="utf-8"?><soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><soap12:Upgrade
> xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:SupportedEnvelope
> qname="soap:Envelope"
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" />
> 
> <soap12:SupportedEnvelope qname="soap12:Envelope"
> xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" />
> 
> </soap12:Upgrade></soap:Header><soap:Body><soap:Fault>
> 
> <faultcode>soap:VersionMismatch</faultcode><faultstring>Possible SOAP
> version mismatch: Envelope namespace https://www.stratexsystems.com/
> was unexpected. Expecting
> http://schemas.xmlsoap.org/soap/envelope/.</faultstring>
> 
> <detail /></soap:Fault></soap:Body>
> 
> </soap:Envelope>

1 个答案:

答案 0 :(得分:1)

我读到肥皂,发现你遗失了什么 这里有一些信息,但我不知道在哪里输入用户名和密码 希望你能弄明白。

NSString *soapMessage = @"<?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>\
<GetUserName xmlns=\"https://www.stratexsystems.com/\" />\
</soap:Body>\
</soap:Envelope>";
NSURL *url = [NSURL URLWithString:@"https://www.stratexlive.com/tenants/commercialbank/_vti_bin/stratexws.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"https://www.stratexsystems.com/GetUserName" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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