0

我在服务器上有 JSON,假设我的 URL 是

https://abc.companyname.com/posts/list/10

当我将该 URL 粘贴到浏览器中时,它会询问我usernamepassword。当我输入这些凭据时,它会返回一些 JSON 格式的数据,如下所示。

[{"id":"5","picture":"myimage.jpg","name":"Usman Zafar","type":"textonly","message":"Woo this is demo and it looks good thanks adeco","image":null,"video_id":null,"datecreated":"2013-10-17 10:14:02","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"6","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:04","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"7","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:24","totalLikes":0,"totalComments":0,"commentsData":null},


{"id":"11","picture":"myimage_9.jpg","message":"Regukar Text comments no.772"}]}]

我的问题

如何在 iPhone 编程中使用身份验证(用户名+密码)创建 NSURLConnection?

如果需要有关问题的任何其他数据,请通过评论告诉我。

谢谢

4

2 回答 2

0

这更多的是应用程序设计。首先,通过向用户显示一些 UI 屏幕来捕获用户名和密码,然后您需要将帖子正文中的数据发送到服务器。您可以将普通键值对转换为 NSData。

从下面的代码开始。

===============================================================================================

self.URLRequest = [NSMutableURLRequest requestWithURL:self.URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:999];
[self.URLRequest setHTTPMethod:[self methodStringForMethod:self.requestMethod]];
    for (NSString *aHeaderKey in self.requestHeader) {
        [self.URLRequest setValue:[self.requestHeader valueForKey:aHeaderKey] forHTTPHeaderField:aHeaderKey];
    }

    if (self.requestHeader) {
        [self.URLRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    }

    if (self.body) {
        [self.URLRequest setHTTPBody:[self keyValuePostDataFromDictionary:self.body]];
    }

    if (self.connection) {
        [self.connection cancel];
        self.connection = nil;
    }
self.connection = [[NSURLConnection alloc] initWithRequest:self.URLRequest delegate:self];
===============================================================================================

- (NSData *)keyValuePostDataFromDictionary:(NSDictionary *)iDictionary {
    return [[NSString runnerHttpPostBodyStringWithQueryParameters:iDictionary] dataUsingEncoding:NSUTF8StringEncoding];
}


- (NSString *)stringByAppendingRunnerQueryParameters:(NSDictionary *)iParameters appendQuestionMark:(BOOL)iAppendQuestionMark {
    BOOL aAppendAmpersand = YES;
    NSMutableString *aWorking = [NSMutableString stringWithString:self];

    if (iAppendQuestionMark) {
        NSRange aQueryBeginning = [self rangeOfString:@"?"];
        if (aQueryBeginning.location == NSNotFound) {
            [aWorking appendString:@"?"];
            aAppendAmpersand = NO;
        }
    } else {
        aAppendAmpersand = NO;  
    }

    for (id aKey in iParameters) {
        id aValue = [iParameters valueForKey:aKey];
        NSString *aKeyStr = [self convertRunnerObjectToURLEncodedValue:aKey];

        if (aAppendAmpersand) {
            [aWorking appendString:@"&"];
        } else {
            aAppendAmpersand = YES;
        }

        if ([aValue isKindOfClass:[NSArray class]]) {
            NSArray *aSubParamaters = (NSArray *)aValue;
            BOOL aFirstTime = YES;
            for (id aSubValue in aSubParamaters) {
                NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aSubValue];

                if (!aFirstTime) {
                    [aWorking appendString:@"&"];                   
                }
                [aWorking appendString:aKeyStr];
                [aWorking appendString:@"="];
                [aWorking appendString:aValueString];

                aFirstTime = NO;
            }

        } else {
            NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aValue];
            [aWorking appendString:aKeyStr];
            [aWorking appendString:@"="];
            [aWorking appendString:aValueString];           
        }

    }

    return [NSString stringWithString:aWorking];        
}
于 2013-10-22T02:52:31.933 回答
0

使用以下代码:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
于 2013-10-22T04:42:32.000 回答