我正在尝试发送带有身份验证安全性的http请求。它将以JSON格式提供响应。我正在寻找发送http请求以获取响应的任何教程或示例代码。我已经搜索但没有获得任何有用的教程请帮助我,提前致谢。
答案 0 :(得分:0)
您可以实施“NSURLConnectionDataDelegate”。在.h文件中执行类似
的操作@interface youClass : NSObject<NSURLConnectionDataDelegate>
在.h文件中实现“NSURLConnectionDataDelegate”
的委托方法- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace: (NSURLProtectionSpace *)protectionSpace {
// NSLog(@"canAuthenticateAgainstProtectionSpace:");
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge {
// NSLog(@"didReceiveAuthenticationChallenge:");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// NSLog(@"connectionDidFinishLoading");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// NSLog(@"Recived data!!!");
}
希望这会有所帮助。 :)
答案 1 :(得分:0)
假设您想要从服务中获取数据,并且您有URl For That,因此以下方法将从Url Given中获取服务器中的数据
- (NSString *)getDataFromService:(NSString *)strUrl {
NSURL *url = [NSURL URLWithString:strUrl];
NSString *response;
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request addBasicAuthenticationHeaderWithUsername:USERNAME
andPassword:PASSWORD];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
response = [request responseString];
}
else
{
response=nil;
}
return response;
}
所以在响应String中你有来自给定网址的服务器的响应数据。
为此你需要从github下载名为ASIHTTPRequest的库这里是链接