2013-02-07 36 views
4

我有一個.Net Web服務,我將它用於移動應用程序。我用它在Windows手機和Android應用程序,但我沒有從iOS獲取數據。例如,我的Web服務中有一個方法,它只需要一個參數。我如何從返回值中獲取數據?我在互聯網上找到了一個例子,我編輯了它,但我無法獲取數據。所有的代碼都在這裏。在這種情況下,我需要示例代碼。感謝您的關注。從iOS上的.net Web服務獲取數據

Web服務的信息

命名空間:http://tempuri.org 網址:http://www.example.com/webservice1.asmx 方法名稱:FirmaGetir 參數名:firID(串)

Web服務請求:

POST /webservice1.asmx HTTP/1.1 
Host: example.com 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 

<?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> 
    <FirmaGetir xmlns="http://tempuri.org/"> 
     <firID>string</firID> 
    </FirmaGetir> 
    </soap12:Body> 
</soap12:Envelope> 

個返回數據

HTTP/1.1 200 OK 
Content-Type: application/soap+xml; charset=utf-8 
Content-Length: length 
<?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> 
    <FirmaGetirResponse xmlns="http://tempuri.org/"> 
     <FirmaGetirResult> 
     <Firma> 
      <firma_adi>string</firma_adi> 
      <adres>string</adres> 
      <telefon>string</telefon> 
      <id>string</id> 
      <sektor>string</sektor> 
      <alt_sektor>string</alt_sektor> 
      <alt_sektor_adi>string</alt_sektor_adi> 
      <servis>string</servis> 
      <map>string</map> 
      <slogan>string</slogan> 
      <sayfaGosterimi>int</sayfaGosterimi> 
      <gpsilce>string</gpsilce> 
      <gpssemt>string</gpssemt> 
      <gpspk>string</gpspk> 
      <duyuru> 
      <baslik>string</baslik> 
      <icerik>string</icerik> 
      <link>string</link> 
      <image>string</image> 
      </duyuru> 
      <firma_link> 
      <tam_adi>string</tam_adi> 
      <kisa_adi>string</kisa_adi> 
      <firma_link>string</firma_link> 
      </firma_link> 
     </Firma> 
     </FirmaGetirResult> 
    </FirmaGetirResponse> 
    </soap12:Body> 
</soap12:Envelope> 

SOAPExampleViewController.h

#import <UIKit/UIKit.h> 
@interface SOAPExampleViewController : UIViewController 
{ 
    NSXMLParser *xmlParser; 
    NSMutableData *webData; 
    NSMutableString *soapResults; 
    BOOL recordResults; 
} 

@property(nonatomic, retain) NSMutableData *webData; 
@property(nonatomic, retain) NSXMLParser *xmlParser; 
@property(nonatomic, retain) NSMutableString *soapResults; 

-(IBAction)buttonClick:(id)sender; 

@end 

SOAPExampleViewController.m

#import "SOAPExampleViewController.h" 

@implementation SOAPExampleViewController 
@synthesize xmlParser, webData, soapResults; 


-(IBAction)buttonClick:(id)sender 
{ 
    NSString *firid = [NSString stringWithFormat:@"800"]; 
    recordResults = NO; 
    NSString *soapMessage = [NSString stringWithFormat: 
         @"POST /webservice1.asmx HTTP/1.1\n" 
         "Host: example.com\n" 
         "Content-Type: application/soap+xml; charset=utf-8\n" 
         "Content-Length: length\n" 

         "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<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\">\n" 
         "<soap12:Body>\n" 
         "<FirmaGetir xmlns=\"http://tempuri.org/\">\n" 
         "<firID>%@</firID>\n" 
         "</FirmaGetir>\n" 
         "</soap12:Body>\n" 
         "</soap12:Envelope>\n",firid]; 
    NSLog(@"%@", soapMessage); 
    NSURL *url = [NSURL URLWithString:@"http://www.example.com/webservice1.asmx?op=FirmaGetir"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [theRequest addValue:@"http://tempuri.org/FirmaGetir" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
    } 
    else 
    { 
     NSLog(@"theConnection is null"); 
    } 
} 

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response 
{ 
    [webData setLength:0]; 
    NSHTTPURLResponse * httpResponse; 
    httpResponse = (NSHTTPURLResponse *) response; 
    NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode); 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data 
{ 
    [webData appendData:data]; 
    //NSLog(@"webdata: %@", data); 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error 
{ 
    NSLog(@"error with the connection"); 
    [connection release]; 
    [webData release]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"DONE. Received bytes %d", [webData length]); 
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"xml %@",theXML); 
    [theXML release]; 
    NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Respose Data :%@",responseString) ; 
    if(xmlParser) 
    { 
     [xmlParser release]; 
    } 
    xmlParser = [[NSXMLParser alloc] initWithData:webData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities:YES]; 
    [xmlParser parse]; 
    [connection release]; 
    [webData release]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
{ 
    if([elementName isEqualToString:@"firma_adi"] || [elementName isEqualToString:@"Firma"] || [elementName isEqualToString:@"adres"]) //I'm trying 
    { 
      if(!soapResults) 
     { 
      soapResults = [[NSMutableString alloc]init]; 
     } 
     recordResults = YES; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if(recordResults) 
    { 
     [soapResults appendString:string]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if([elementName isEqualToString:@"firma_adi"] || [elementName isEqualToString:@"Firma"] || [elementName isEqualToString:@"adres"]) // I'm trying 
    { 
     recordResults = NO; 
     NSLog(@"%@", soapResults); 
     [soapResults release]; 
     soapResults = nil; 
    } 
} 
+0

您的網址重定向到其他頁面。給實際的網址。 –

+0

是否確定這適用於您,因爲您忘記將** **添加到.h文件以進行xml解析。 –

回答

1

變化http://tempuri.org/FirmaGetir在SOAPAction的http://tempuri.org/ServisKontrol。 你也可以參考這個鏈接瞭解更多信息。 iPhone interaction with ASP.NET WebService

+0

對不起,這是我的錯。我改變了我的代碼,但仍然無法獲取數據。 –

+0

你的肥皂信息中還有一些問題,請參考我的鏈接 –

2

由這一個替換下面

NSURL *url = [NSURL URLWithString:@"http://www.example.com/webservice1.asmx"]; 

線。

NSURL *url = [NSURL URLWithString:@"http://www.example.com/webservice1.asmx/ServisKontrol"]; 

這將有所幫助。

+0

沒有任何改變 –

2

或者只是嘗試一個很棒的免費工具,它會生成您需要的所有類和代理。 wsdl2code.com