2013-03-18 35 views
1

如何解析包含userName和Password的url。用iPhone中的用戶名和密碼解析url

基本上我知道的XML解析...但我不與獲得任何數據..

誰能幫我提前解析這樣的網址...

謝謝..

+0

你從你的服務器 – 2013-03-18 09:07:39

+1

得到什麼類型的數據可以爲您詳細說明你的問題? – 2013-03-18 09:07:39

+0

在獲取服務響應或解析響應時出現問題時,您是否遇到問題? – 2013-03-18 09:10:05

回答

4

可能有兩個問題: -

1當前未接收到正確的數據,因爲你沒有給密碼&用戶名在委託

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 

實現它像這是NSURLConnection的委託(如果你使用它)。

#define LOGIN  @"RFC_ESERVICE" 
#define PASSWORD @"[email protected]" 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
     if ([challenge previousFailureCount] == 0) 
     { 
      NSURLCredential *credential = [NSURLCredential credentialWithUser:LOGIN 
                    password:PASSWORD 
                    persistence:NSURLCredentialPersistenceNone]; 
      [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
     } 
} 

2如果您收到數據但無法解析它,請發佈代碼。 此外,XML似乎是一個WSDL,你想分析哪些信息?

3

這是你如何使用的NSXMLParser:

In your .h file declare : 

NSMutableData  *webPortFolio; 
NSMutableString  *soapResultsPortFolio; 
NSURLConnection  *conn; 

//---xml parsing--- 

NSXMLParser   *xmlParserPortFolio; 
BOOL    elementFoundPortFolio; 
NSMutableURLRequest *req; 

NSString   *theXMLPortFolio; 
NSString   *strSoapMsg; 
UIAlertView   *alertView; 

在您.m文件使用下面的代碼:

-(void)callURL 
{ 
     NSString *soapMsg = [NSString stringWithFormat:@"email=%@&pass=%@&type=activate",txt_UserName.text,txt_Password.text]; //Add your parameters here. 

     //---print it to the Debugger Console for verification--- 


     NSString *str_url = [ NSString stringWithFormat:@"%@login",xmlWebservicesUrl]; //Your URL here 
     NSURL *url = [NSURL URLWithString:str_url]; 
     req = [NSMutableURLRequest requestWithURL:url]; 

     NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]]; 
    [req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 

     [req setHTTPMethod:@"POST"]; 
     [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 
    //Your logic to call URL. 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    if (conn) 
    { 
     webPortFolio = [[NSMutableData data] retain]; 
    } 
} 
And to handle the response you can use following functions : 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 

} 

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{ 

} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 
} 

//---when the start of an element is found--- 
-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
    namespaceURI:(NSString *) namespaceURI 
    qualifiedName:(NSString *) qName 
    attributes:(NSDictionary *) attributeDict 
{ 
} 

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string 
{ 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{ 
    NSLog(@"Parser error %@ ",[parseError description]); 
} 


//---when the end of element is found--- 
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
{ 
} 
1
NSString *[email protected]"https://ecservices.wasl.ae/sap/bc/srt/wsdl/bndg_514403C105C32C67E10000000AF00316/wsdl11/allinone/ws_policy/document?sap-client=100"; 

NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlSt]]; 

NSString *authStr = [NSString stringWithFormat:@"RFC_ESERVICE:[email protected]"]; 
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding]; 
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]]; 
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"]; 

NSLog(@"Request is %@",theRequest); 

[_webview loadRequest:theRequest];