#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. ubuntu18.04+ cuda9.0+opencv3.1+caffe-ssd安装

    详细Ubuntu18.04,CUDA9.0,OpenCV3.1,Tensorflow完全配置指南 问题1:使用Cmake编译opencv源码 CMake Error: The following va ...

  2. vRealize Automation的REST API Reference在哪里可以看到?

    两个地方: 1. VMware官网可以查看. http://pubs.vmware.com/vrealize-automation-71/topic/com.vmware.vra.restapi.do ...

  3. ios 内存管理总结

    在ios 中 项目有两个内存管理方式 第一种,arc 方式,编译器编译时,自动给obj 加上 release  实现要求 1. 设置项目 将 Objective-C Automatic Referen ...

  4. (文档)Shader.Find (在编译时,只包含那些使用中的shader或位置在"Resources"文件夹中shader)

    Shader.Find 查找 static function Find (name : string) : Shader Description描述 Finds a shader with the g ...

  5. java interface 默认值

    /* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...

  6. eclipse插件大全

    http://www.cnblogs.com/homezzm/archive/2009/11/27/1612054.html

  7. Word2007视频教程

    超级好教程 http://v.youku.com/v_show/id_XMTAwOTgwNTIw.html 视频: oeasy教你玩转office系列之Word视频教程01 http://v.youk ...

  8. 第六周 Word目录和索引

    第六周 Word目录和索引 教学时间 2013-4-2 教学课时 2 教案序号 5 教学目标 能正确使用索引.目录等 教学过程: 复习提问 1.脚注和尾注的区别是什么?2.如何插入脚注和尾注?3.如何 ...

  9. SHELL异常处理

    写SHELL好久了,经常被异常困扰,可竟然坚持了若干年没用过,回想以前服务过的公司,阿弥陀佛,罪过罪过.废话少说,希望此篇文章可以协助大家和我彻底结束SHELL脚本就是LINUX命令集合的初级阶段. ...

  10. JAVA i++于++i的区别

    大家看一下下面一个程序: 一.问题说明 Test.java public class Test { public static void main(String[] args) { int i = 1 ...