Authentication 如何将NSURLConnection sendSynchronousRequest更改为包含HTTPS连接的基本身份验证

Authentication 如何将NSURLConnection sendSynchronousRequest更改为包含HTTPS连接的基本身份验证,authentication,https,nsurlconnection,Authentication,Https,Nsurlconnection,目前,下面的代码是我必须与服务器建立同步HTTP连接的代码。 服务器基本上会根据查询显示所需的UI对话框。 现在,服务器更改为需要基本身份验证(用户名和密码)的安全服务器。 你能帮我举个例子,把它改成带有基本身份验证的HTTPS请求吗 NSString *url = localhosturl; NSString *path=[items[0] path]; path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCh

目前,下面的代码是我必须与服务器建立同步HTTP连接的代码。 服务器基本上会根据查询显示所需的UI对话框。 现在,服务器更改为需要基本身份验证(用户名和密码)的安全服务器。 你能帮我举个例子,把它改成带有基本身份验证的HTTPS请求吗

NSString *url = localhosturl;

NSString *path=[items[0] path];
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];

NSArray *myStrings = [[NSArray alloc] initWithObjects:url, syncportnum, query, path, nil];
NSString *targetUrl = [myStrings componentsJoinedByString:@""];


NSURL *url_1 = [NSURL URLWithString:targetUrl];

NSURLRequest *request1 = [NSURLRequest requestWithURL:url_1];


NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// check for an error. If there is a network error, you should handle it here.
if(!error)
{
    //log response
    NSLog(@"Response from server = %@", responseString);
}

谢谢大家!

这通常意味着将URL更改为

https://username:password@主机名/…

。。。有一长串的警告,例如用户名和密码将不可避免地出现在缓存中,强烈建议重写此项以使用支持的API异步执行相同的工作。:-)