#import <Foundation/Foundation.h>
#import "ASIFormDataRequest.h" @protocol NetWorkdelegate;
@interface JSNetWork : NSObject<ASIHTTPRequestDelegate>
+(JSNetWork *)shareNetWork; -(void)JsNetWordWithConnectId:(int)connectid Body:(NSString *)body PostBody:(NSString *)PostBody Delegate:(id<NetWorkdelegate>)delegate;
@end @protocol NetWorkdelegate <NSObject>
-(void)NetWorkwithConnectId:(int)connectid backstring:(NSString *)aback;
@end @implementation JSNetWork +(JSNetWork *)shareNetWork{
static dispatch_once_t onceToken;
static JSNetWork *netWork=nil;
dispatch_once(&onceToken, ^{
netWork=[[JSNetWork alloc] init];
});
return netWork;
} #pragma mark -request
-(void)JsNetWordWithConnectId:(int)connectid Body:(NSString *)body PostBody:(NSString *)PostBody Delegate:(id<NetWorkdelegate>)delegate{
if (![self Isconnect]) {
UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:nil message:@"not work" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alertview show];
[alertview release];
return;
}
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:body]];
request.timeOutSeconds=;
request.delegate=self;
request.requestMethod=@"POST";
NSArray *Bodyarry=[PostBody componentsSeparatedByString:@"&"];
for(NSString *tmp in Bodyarry){
NSArray *temparrary=[tmp componentsSeparatedByString:@"="];
[request setPostValue:temparrary[] forKey:temparrary[]];
}
NSString *cid=[NSString stringWithFormat:@"%zi",connectid];
NSDictionary *dic=@{@"connectid":cid,@"delegate":delegate};
[request setUserInfo:dic];
[request startAsynchronous]; } -(void)requestFinished:(ASIHTTPRequest *)request{
NSDictionary *userinfo=request.userInfo;
int connectid=[userinfo[@"connectid"] intValue];
id<NetWorkdelegate> delegate=userinfo[@"delegate"];
NSString *str=[request responseString];
[delegate NetWorkwithConnectId:connectid backstring:str];
} -(void)requestFailed:(ASIHTTPRequest *)request{ } -(BOOL)Isconnect{
BOOL iscon=NO;
Reachability *r=[Reachability reachabilityWithHostName:@"www.baidu.com"];
switch ([r currentReachabilityStatus]) {
case NotReachable:
NSLog(@"not network");
iscon=NO;
break;
case ReachableViaWiFi:
NSLog(@"wifi");
iscon=YES;
break;
case ReachableViaWWAN:
NSLog(@"3g");
iscon=YES;
break;
default:
break;
}
return iscon;
} @end
- (void)viewDidLoad
{
[super viewDidLoad];
[[JSNetWork shareNetWork] JsNetWordWithConnectId: Body:@"http://www.baidu.com" PostBody:nil Delegate:self];
// Do any additional setup after loading the view, typically from a nib.
NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
path=[path stringByAppendingPathComponent:@"1.plist"];
NSString *str=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
if(str.length>)
[self NetWorkwithConnectId: backstring:str];
} -(void)NetWorkwithConnectId:(int)connectid backstring:(NSString *)aback{
self.txt.text=aback;
[self Save:aback];
} -(void)Save:(NSString *)str{
NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
path=[path stringByAppendingPathComponent:@"1.plist"];
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
} - (void)dealloc {
[_txt release];
[super dealloc];
}

ios中网络请求缓存的更多相关文章

  1. ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)

    当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...

  2. iOS开发网络请求——大文件的多线程断点下载

    iOS开发中网络请求技术已经是移动app必备技术,而网络中文件传输就是其中重点了.网络文件传输对移动客户端而言主要分为文件的上传和下载.作为开发者从技术角度会将文件分为小文件和大文件.小文件因为文件大 ...

  3. IOS中http请求使用cookie

    http://rainbird.blog.51cto.com/211214/805173 IOS中http请求使用cookie 2012-03-13 23:04:30 标签:http 记录 龙的传人  ...

  4. 聊聊iOS中网络编程长连接的那些事

    1.长连接在iOS开发中的应用 常见的短连接应用场景:一般的App的网络请求都是基于Http1.0进行的,使用的是NSURLConnection.NSURLSession或者是AFNetworking ...

  5. Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析

    Java生鲜电商平台-SpringCloud微服务架构中网络请求性能优化与源码解析 说明:Java生鲜电商平台中,由于服务进行了拆分,很多的业务服务导致了请求的网络延迟与性能消耗,对应的这些问题,我们 ...

  6. iOS网络——NSURLCache设置网络请求缓存

    今天在看HTTP协议,看到了response头中的cache-control,于是就深入的研究了一下.发现了iOS中一个一直被我忽略的类——NSURLCache类. NSURLCache NSURLC ...

  7. iOS - AFNetworking 网络请求

    前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...

  8. iOS - ASIHTTPRequest 网络请求

    前言 使用 iOS SDK 中的 HTTP 网络请求 API,相当的复杂,调用很繁琐,ASIHTTPRequest 就是一个对 CFNetwork API 进行了封装,并且使用起来非常简单的一套 AP ...

  9. iOS - NSURLSession 网络请求

    前言 NS_CLASS_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0) @interface NSURLSession : NSObject @available(iOS ...

随机推荐

  1. solr高亮设置以及摘要

    高亮显示 public SolrDocumentList query(String str) { SolrQuery query = new SolrQuery(str); //设置高亮,以下两种方式 ...

  2. Windows 安装配置 JIRA

    MySQL-5.5.28 JDK1.6.0_21 JIRA功能全面,界面友好,安装简单,配置灵活,权限管理以及可扩展性方面都十分出色. 一.MySQL建库和建账号 1. mysql中创建数据库jira ...

  3. JavaScript学习笔记——错误处理

    说明:参见<JavaScript高级程序设计>第14章. 一.             错误分类 1. 语法错误 也称为解析错误,发生在传统编程语言的编译时,在JavaScript中发生在 ...

  4. 【RPC】跨语言-RPC框架

    跨语言-RPC框架 跨语言 rpc_百度搜索 (5 条消息)谁能用通俗的语言解释一下什么是 RPC 框架? - 知乎 跨语言RPC框架Hessian.Thrift.Protocol Buffer之间的 ...

  5. sql-获取指定年份指定月份的天数

    declare @年月 varchar(6) set @年月= '201803' --查询2015年2月有多少天 select day(dateadd(month,1,@年月+ '01 ')-1)

  6. Eclipse QuickSear的插件的说明

    https://spring.io/blog/2013/07/11/eclipse-quick-search Eclipse QuickSear的插件的说明

  7. ifdef ANDROID总是不好用

    在vs的proprocessor definition中定义了ANDRIOD, 但是执行下面的代码的时候,总是说ANDROID is not defined.     #ifdef ANDROID   ...

  8. java 让图片变黑白

    import java.awt.Image;import java.awt.color.ColorSpace;import java.awt.image.BufferedImage;import ja ...

  9. Android 多语言支持

    本文内容 字符串本地化原理 环境 创建项目 测试其他语言 Android 本地化语言 ISO 编码 参考资料 使用 Android 的人越来越多,每天都在增加.因此,当你想把你的应用成功地全球化时,通 ...

  10. java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)

    拂晓风起 专注前端技术cocos2d.js.flash.html5,联系:kenkofox@qq.com.如果读者要找腾讯工作机会,请不吝推荐简历. 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔 ...