HttpBasicAuthentication in IPhone

后端 未结 2 1415
一个人的身影
一个人的身影 2021-01-01 08:01

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 ask

2条回答
  •  既然无缘
    2021-01-01 08:30

    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.

提交回复
热议问题