我在基本身份验证中遇到问题,对此我无法理解出现此问题的原因。在基本身份验证中,我得到响应代码 200,即身份验证成功,但有时我无法在 iOS 应用程序中正确获取响应数据。如果我在浏览器中粘贴请求 url(即使在 iPhone safari 浏览器中也是如此),我总是得到响应。但是为什么我并不总是在 iOS 应用程序中得到响应。
我在 google 和堆栈溢出中遇到了许多其他问题,例如link1,但没有运气。
我的代码如下:
//in implemetation file
-(void)basicAuthentication{
.
.
NSURL *url = [NSURL URLWithString:authenticationUrl];
NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", @"username",@"password"];
NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", loginString];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request addValue:authHeader forHTTPHeaderField:@"Authorization"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
myData = [[NSMutableData data] retain];
}
//Delegate method called when the connection receives data
/*
* Callback, called when ever there is more response data read
*/
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myData appendData:data];
}
//in .h file
@interface TestConnectionManager : NSObject <UIAlertViewDelegate,NSURLConnectionDelegate>{
id <TestConnectionManagerDelegate>delegate;
NSMutableData *myData;
.
.
.
}