2012-07-12 64 views
0

我遇到問題連接到此webservice,有什麼想法我做錯了什麼?如何讓iOS與.NET網絡服務交談

- (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:@"[email protected]"]; 
    [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

什麼'問題'?... – 2012-07-12 15:42:19

+0

你不能使用NSURLConnection? – 2012-07-12 15:44:28

+0

@ H2CO3已更新問題。 Inder - 我的請求將變得更加複雜,所以我更熟悉ASIHTTREQUEST – TheLearner 2012-07-12 15:46:35

回答

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]; 
+1

你在這裏正確的軌道上。根本問題是您必須構建* entire * SOAP消息,包括信封。 OP的要求只包括機構。我無法強調在ObjC中手工編制和手動解碼SOAP的複雜請求是多少。如果切換到REST是不可能的,那麼我建議使用gsoap而不是手動構建這些東西。 – 2012-07-12 16:20:53