2012-03-01 92 views
2

我真的很新的Xcode,我需要連接到一個Web服務API(SOAP + DES),這意味着我必須發送一個用戶名,密碼,密鑰和IV到服務器才能獲得XML文件並解析它。如何從iOS連接到Web服務(SOAP + DES)?

有沒有關於從iPhone連接到Web服務API(SOAP + DES)的任何教程?

-(IBAction)buttonClick:(id)sender 
{ 
    recordResults = NO; 

    NSString *soapMessage = [NSString stringWithFormat: 
    @"<?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:Header>" 
    "<CredentialSoapHeader xmlns=\"http://tempuri.org/\">" 
    "<userID>xxxxx</userID>" 
    "<Password>xxxx</Password>" 
    "</CredentialSoapHeader>" 
    "</soap:Header>" 
    "<soap:Body>" 
    "<GetNearUserByArticleLatLon xmlns=\"http://tempuri.org/\">" 
    "<strXML>" 
          "<XMLDATA>\n" 
          "<KeyWord></KeyWord>\n" 
          "<Lat>0</Lat>\n" 
          "<Lon>0</Lon>\n" 
          "<IP>219.71.65.55</IP>\n" 
          "<CurrentPage>1</CurrentPage>\n" 
          "<PageSize>10</PageSize>\n" 
          "</XMLDATA>\n" 
          "</strXML>" 
    "</GetNearUserByArticleLatLon>" 
    "</soap:Body>" 
    "</soap:Envelope>"]; 


    NSLog(@"%@", soapMessage); 

    //http://www.xignite.com/services/GetRealQuote 
    NSURL *url = [NSURL URLWithString:@"http://api3.mmnears.com/API/api.asmx?op=GetNearUserByArticleLatLon"]; 
    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/GetNearUserByArticleLatLon" forHTTPHeaderField:@"SOAPAction"]; 
    [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 


    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    NSLog(@"theConnection = %@",theConnection); 

    if(theConnection) 
    { 
     webData = [[NSMutableData data] retain]; 
     NSLog(@"WebData = %@",webData); 

    } 
    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]; 
+0

是啊^^ 將這兩件事'<' and '>'更改爲措辭'&lt;和'&gt; – 2012-04-23 11:19:32

回答

0

看到這個SOAP連接教程: SOAPExample

SOAP Reference

+0

嗨Hector〜我得到HTTP錯誤400>< – 2012-03-02 03:12:14

+0

你使用代理服務器嗎?如果你的數據放在另一臺服務器上,而不是繞過它的代理服務器。 – Hector 2012-03-02 03:48:51

+0

仍然不能正常工作><我沒有使用代理服務器。 – 2012-03-02 04:30:30