2016-01-19 98 views

回答

0

我在wordpress博客中添加了iOS中的示例SOAP實現。希望這會幫助你https://wordpress.com/post/bharathreddys.wordpress.com/6

讓我們假設以下.wsdl url是源URL。

http://www.webservicex.net/geoipservice.asmx?WSDL 

創建SOAP信封和SOAP標頭。

NSMutableString * soapHeader = @"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" 

     " xmlns:hom=\"http://www.webservicex.net/">" 

     "<soapenv:Header/>"; 

被記住「的xmlns:坎」,旁邊的文「:」是一個將被用作將在SOAP體中添加服務調用的targetNamespace引用的一句話。

SOAP體將作如下安排:

[soapHeader appendString:@"<soapenv:Body>"]; 

[soapHeader appendString:@"<hom:GetGeoIP>"]; // add the body parameters 

[soapHeader appendString:@"<hom:IPAddress>0.0.0.1</hom:IPAddress>"]; 

[soapHeader appendString:@"</hom:GetGeoIP>"]; 

[soapHeader appendString:@"</soapenv:Body>"]; 

[soapHeader appendString:@"</soapenv:envelope>"]; 

現在的Web服務如下:

NSMutableString * requestStr = [NSMutableStringstring]; 

[requestStr appendString:soapHeader]; 

NSString * urlStr = @"http://www.webservicex.net/geoipservice.asmx?WSDL"; 

NSURL * url = [NSURL URLWithString:urlStr]; 

NSMutableURLRequest * request = [[NSMutableURLRequestalloc] initWithURL:url]; 

[request setValue:@"http://www.webservicex.net/GetGeoIP" forHTTPHeaderField:@"SOAPAction"]; 

[request setValue:@"http://www.webservicex.net/" forHTTPHeaderField:@"targetNamespace"]; 

[request setValue:@"text/xml;charset=UTF-8"forHTTPHeaderField:@"Content-Type"]; 

[request setHTTPBody:requestBody]; 

[request setHTTPMethod:@"POST"]; 

[request setValue:[NSStringstringWithFormat:@"%d",[requestBody length]] forHTTPHeaderField:@"Content-Length"]; 

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

    BOOL isSuccess = YES; 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    NSLog(@"received data---%@",response); 

    if(connectionError != nil) { 

     isSuccess = NO; 

     NSLog(@"connection error with code %d",connectionError.code); 

    } 

    NSDictionary * responseDictionary = [NSDictionary dictionary]; 

    if([httpResponse statusCode] == 200) { 

     isSuccess = YES; 

    //Do something with the received dictionary. 

}]; 

這是基本的平臺,我們如何才能使一個簡單的基於SOAP的Web服務調用iOS版。

快樂編碼!!!!

+0

我沒有wordpress帳戶,你可以在這裏粘貼答案嗎? –

+0

查看我上面更新的答案。 –