HttpBasicAuthentication in IPhone

蹲街弑〆低调 提交于 2019-12-18 13:48:20

问题


In my iPhone app, I am trying to display an image on iphone from my server, which needs authorization. I am trying to use NSURLConnection to get the image, but it is not asking me for the user credentials. i.e it is not at all going to didReceiveAuthenticationChallenge method. Can anyone help me with this issue?


回答1:


You can compute the necessary Authorization header yourself and manually apply it to the outgoing NSURLRequest before creating the NSURLConnection, as in:

NSMutableURLRequest *someURLReq = ...
NSString *auth = ...
[someURLReq setValue:auth forHTTPHeaderField:@"Authorization"];

The content of the auth in the case of HTTP basic authentication, using Dave Dribin's base64 NSData category would be:

NSString *username = ...
NSString *password = ...
NSString *combo = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *comboData = [NSData dataWithBytes:[combo UTF8String] length:combo.length];
NSString *auth = [NSString stringWithFormat:@"Basic %@", [comboData encodeBase64]];

Note that this is not encryption, the password is plain text for all practical purposes, and will be sniffed unless you're on an SSL connection.




回答2:


Since your not posting any code its hard to tell. I would double check and make sure that you have the delegate member set properly on the NSURLConnection. If that is not the issue try using an HTTP debugger and make sure that the URL is actually triggering the authentication request.



来源:https://stackoverflow.com/questions/761445/httpbasicauthentication-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!