在iPhone中使用用户名和密码解析网址

时间:2013-03-18 09:03:02

标签: iphone ios objective-c xcode ipad

如何解析包含userName和Password的网址。

基本上我知道XML解析...但是我没有得到任何数据......

任何人都可以帮我解析这样的网址......

提前致谢..

3 个答案:

答案 0 :(得分:4)

可能有两个问题: -

1您没有收到正确的数据,因为您没有提供密码&代理中的用户名

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

像它一样实现它作为NSURLConnection的委托(如果你使用它)。

#define LOGIN     @"RFC_ESERVICE"
#define PASSWORD  @"adm5ls@w"

- (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,您要解析哪些信息?

答案 1 :(得分: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
{
}

答案 2 :(得分:1)

NSString *urlSt=@"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:adm5ls@w"];
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];