0

如何解析包含用户名和密码的 url。

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

谁能帮我解析这样的网址...

提前致谢..

4

3 回答 3

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 ,你想解析什么信息?

于 2013-03-18T09:11:02.037 回答
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
{
}
于 2013-03-18T09:21:53.547 回答
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];
于 2013-03-18T09:40:16.517 回答