| ASIHTTPRequest Version | Minimum iOS Target | Target Notes |
|---|---|---|
| 1.8.1 -> 1.8.2 | iOS 3.0+ | |
| 0.2 -> 1.8.0 |
// 添加系统库文件 CFNetwork.framework SystemConfiguration.framework MobileCoreServices.framework CoreGraphics.framework libz.1.1.3.tbd libxml2.2.tbd // 添加第三方库文件 ASIHTTPRequest-1.8.2 // 在 TARGETS -> Builed Settings -> Search Paths -> Header Search Paths 中添加文件路径 /usr/include/libxml2 // 在 TARGETS -> Build Phases -> Compile Sources -> ...in .../ASIHTTPRequest 后添加 -fno-objc-arc // 包含头文件 #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h"
// 设置请求头
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"]];
[request addRequestHeader:@"Referer" value:@"http://www.dreamingwish.com/"];
// 设置应用后台运行时是否仍然请求数据
request.shouldContinueWhenAppEntersBackground = YES;
// 设置请求超时时重试的次数
request.numberOfTimesToRetryOnTimeout = 3;
// 设置 KeepAlive 支持
// Set the amount of time to hang on to a persistent connection before it should expire to 2 minutes
request.persistentConnectionTimeoutSeconds = 120;
// Disable persistent connections entirely
request.shouldAttemptPersistentConnection = NO;
// 设置是否显示网络请求信息在 status bar 上
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO];
// 网络状态检查
BOOL isNetworkInUse = [ASIHTTPRequest isNetworkInUse];
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
// 创建请求
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置超时时间,可不设置,使用默认
request.timeOutSeconds = 5;
// 发送同步请求
[request startSynchronous];
// 获得错误信息
NSError *error = [request error];
// 网络请求失败
if (error) {
// 网络请求成功
NSLog(@"网络请求失败:\n%@", error);
} else {
// 获得服务器的响应,字符串格式
NSString *responseString = [request responseString];
NSLog(@"网络请求成功:\n%@", responseString);
// 获得服务器的响应,NSData 格式
NSData *responseData = [request responseData];
textView.text = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
}
NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com/wp-content/uploads/2011/10/asihttprequest-auth.png"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置文件存储路径
[request setDownloadDestinationPath:@"/Users/JHQ0228/Desktop/asi.png"];
[request startSynchronous];
// 获得错误信息
NSError *error = [request error];
// 网络请求失败
if (error) {
NSLog(@"网络请求失败:\n%@", error);
} else {
// 网络请求成功
NSLog(@"网络请求成功:\n");
}
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
// 创建请求
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置超时时间,可不设置,使用默认
request.timeOutSeconds = 5;
// 设置代理,需遵守 <ASIHTTPRequestDelegate> 协议
request.delegate = self;
// 发送异步请求
[request startAsynchronous];
// 网络请求成功,协议方法
- (void)requestFinished:(ASIHTTPRequest *)request {
}
// 网络请求失败,协议方法
- (void)requestFailed:(ASIHTTPRequest *)request {
}
NSURL *url = [NSURL URLWithString:@"http://api.hudong.com/iphonexml.do?type=focus-c"];
// 创建请求,加 __weak 除去 block 循环调用警告
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// 设置超时时间,可不设置,使用默认
request.timeOutSeconds = 5;
// 发送异步请求
[request startAsynchronous];
// 网络请求成功
[request setCompletionBlock:^{
}];
// 网络请求失败
[request setFailedBlock:^{
}];
// 通常数据是以 'application/x-www-form-urlencoded' 格式发送的,如果上传了二进制数据或者文件,那么格式将自动变为 ‘multipart/form-data'。
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]];
// 没有文件
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
// 发送文件
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
// 数据的 mime 头是自动判定的,但是如果你想自定义mime头,那么这样:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]];
// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
// Upload an NSData instance
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"myphoto.jpg"]);
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
// 你可以使用 addPostValue 方法来发送相同 name 的多个数据:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.dreamingwish.com"]]; [request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]]; // Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody: [request setRequestMethod:@"PUT"];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有