在URL中传递用户名和密码以进行身份​​验证

时间:2013-02-11 13:21:22

标签: iphone ios url nsurl

我希望在URL(Web服务)中传递用户名和密码以进行用户身份验证,这将返回true和false。我这样做如下:

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中传递我的用户名和密码以发出请求。 请问任何人建议或提供一些示例代码? 感谢。

4 个答案:

答案 0 :(得分:0)

试试这个: -

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"];

希望它可以帮助你..

答案 1 :(得分:0)

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

答案 2 :(得分:0)

首先,我不会在网址中传递用户名和密码。你应该使用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];

这比在网址中传递敏感数据更安全。

修改

要获得回复,您可以查看此信息。 NSURLConnectionAppleDoc 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);

}];

答案 3 :(得分:0)

为了提高安全性,您可以使用Http Basic Authentication。 有答案here