Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSMutableURLRequest中的基本授权是';行不通_Objective C - Fatal编程技术网

Objective c NSMutableURLRequest中的基本授权是';行不通

Objective c NSMutableURLRequest中的基本授权是';行不通,objective-c,Objective C,我正在尝试将基本授权添加到我的请求头中。下面的代码已编译,我没有收到任何运行时错误。然而,在服务器端,我根本看不到头中的“授权” 我能够实现didReceiveAuthenticationChallenge方法,而且它是有效的,但是我不明白为什么我必须这样做。我只是希望总是向每个请求添加基本身份验证 我对使用AsitpRequest不感兴趣 谢谢你的帮助 下面是我的代码: NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:

我正在尝试将基本授权添加到我的请求头中。下面的代码已编译,我没有收到任何运行时错误。然而,在服务器端,我根本看不到头中的“授权”

我能够实现didReceiveAuthenticationChallenge方法,而且它是有效的,但是我不明白为什么我必须这样做。我只是希望总是向每个请求添加基本身份验证

我对使用AsitpRequest不感兴趣

谢谢你的帮助

下面是我的代码:

NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:8000/MyWebService"];
self.userName = @"myusername";
self.password = @"mypassword";
NSMutableString *credentials = [[NSMutableString alloc] initWithFormat:@"%@:%@", userName, password];
NSString *encodedCredentials = [[credentials dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString *authHeader =  [NSString stringWithFormat:@"Basic %@", encodedCredentials];
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
[req addValue:authHeader forHTTPHeaderField:@"Authorization"];  
self.urlConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
if (self.urlConnection) {
    self.receivedData = [NSMutableData data];        
}
else  {
    errorLabel.text = @"Error connecting to the server";
}

didReceiveAuthenticationChallenge是它在iOS中的工作方式,也是最简单的方式。它将与每个请求(连同质询)一起提交。我猜这不是你想听的=)

我想你试过使用不同的教程。我以前用过这个进行身份验证: