我在服务器上有JSON,假设我的网址是
https://abc.companyname.com/posts/list/10
当我在浏览中粘贴该网址时,它会向我询问 用户名 和 密码 。当我输入这些凭据时,它会返回一些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?
如果需要有关于问题的任何其他数据,请通过评论告诉我。
由于
答案 0 :(得分: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];
}
答案 1 :(得分:0)
使用以下代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];