我对私有(private)网络服务有疑问。我的意思是,为了访问此网络服务,我需要用户名。我如何才能访问我的网络服务内部。您有什么建议吗?
要有视觉效果 -- http://www.test.com/event (这是一个例子)
-(void)startConnection
{
NSString *urlString = GET_EVENT_URL_STRING;
NSURL *url = [NSURL URLWithString:urlString];
if (!url)
{
NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@", GET_EVENT_URL_STRING];
[self.delegate didGetEventInCorrect:reason];
return;
}
theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];
// Set the HTTP method of the request to POST
[theRequest setHTTPMethod:@"POST"];
if (!theRequest)
{
NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@", GET_EVENT_URL_STRING];
[self.delegate didGetEventInCorrect:reason];
return;
}
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection)
{
NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@", GET_EVENT_URL_STRING];
[self.delegate didGetEventInCorrect:reason];
return;
}
if (theConnection)
{
myData = [[NSMutableData alloc]init];
}
}
例如,当我单击此链接时,我会看到一个带有用户名和密码屏幕的警报..当我输入我可以访问的信息时..此代码用于连接我的网络服务,我该如何管理它?
最佳答案
您需要向 theRequest
对象添加身份验证 header 。
NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
关于ios - 加密 iOS Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693291/