一、ASIHttpRequest组件的添加在我的其他Blog里有介绍,这里不予赘述。
二、基本的request
- (IBAction)grabURL:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
二、带Basic验证的request
【添加】(1)设置request模式为Basic,[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
(2)添加userName和password,[requestaddBasicAuthenticationHeaderWithUsername:@"userName"
andPassword: @"password" ];
//登录操作
- (void) login
{
NSURL *url = [NSURL URLWithString:@"http://106.187.91.215:9412/users/login.json"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
request.shouldPresentCredentialsBeforeChallenge = YES; // NO 表示取消Basic验证
[request addBasicAuthenticationHeaderWithUsername: email.text
andPassword: password.text ];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
//......
}
}
本文详细介绍了如何使用ASIHttpRequest组件发起HTTP请求,并通过示例展示了如何实现基本请求及带有Basic验证的登录操作。
1500

被折叠的 条评论
为什么被折叠?



