在URL中传递用户名和密码进行身份validation

我想在URL(Web服务)中传递用户名和密码进行用户authentication,这将返回true和false.I'm这样做如下:

NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName]; NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword]; NSData *getUserData = [userName dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *getUserLength = [NSString stringWithFormat:@"%d",[getUserData length]]; NSData *getPassData = [passWord dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *getPassLength = [NSString stringWithFormat:@"%d",[getPassData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; [request setURL:[NSURL URLWithString:@"http://URL/service1.asmx"]]; [request setHTTPMethod:@"GET"]; 

现在,我想知道如何在此URL中传递我的用户名和密码以提出请求。 任何人都可以请build议或给一些示例代码? 谢谢。

尝试这个 :-

 NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName.text]; NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword.text]; NSData *getUserData = [userName dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *getUserLength = [NSString stringWithFormat:@"%d",[getUserData length]]; NSData *getPassData = [passWord dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *getPassLength = [NSString stringWithFormat:@"%d",[getPassData length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://URL/service1.asmx?%@&%@",userName,passWord]]]; [request setHTTPMethod:@"GET"]; 

希望它可以帮助你..

 NSString *urlStr = [NSString stringWithFormat:@"http://URL/service1.asmx?%@&%@",userName,passWord]; [request setURL:[NSURL URLWithString:urlStr]]; 

为了提高安全性,您可以使用Http 基本authentication 。 这里有答案。

首先,我不会在URL中传递用户名和密码。 你应该使用post来做到这一点。

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://URL/service1.asmx?"]]; NSString *userName = [NSString stringWithFormat:@"parameterUser=%@",txtUserName]; NSString *passWord = [NSString stringWithFormat:@"parameterPass=%@",txtPassword]; NSString *postString = [NSString stringWithFormat:@"username=%@&password=%@",userName, passWord]; NSData *postData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]]; //URL Requst Object NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:TIMEOUT]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody: postData]; 

这是更安全的,然后将敏感数据传递到一个url。

编辑

要得到答复,你可以检查一下。 NSURLConnection和AppleDoc NSURLConnection

您可以使用几种不同的方法来处理来自服务器的响应。 你可以使用NSURLConnectionDelegate

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [self.connection start]; 

随着代表回电话:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"didReceiveData"); if (!self.receivedData){ self.receivedData = [NSMutableData data]; } [self.receivedData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"connectionDidFinishLoading"); NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"receivedString:%@",receivedString); } 

或者你也可以使用NSURLConnection sendAsynchronousRequest

 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSString *receivedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"receivedString:%@",receivedString); }];