用ASIHTTPRequest进行的封装 虽然很简单,没啥东西 但是还是记一下,以后用到更深的可以再更新一下
ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:url]];
ASIDownloadCache// 封装HTTP的请求头
[request addRequestHeader:@"Content-Type" value:@"application/json; charset=UTF-8"];
[request addRequestHeader:@"Accept" value:@"application/json, text/javascript, */*; q=0.01;version=v2.0"];
[request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d",[url length]]];
//请求包的内容 string 转换UTF8编码
[request appendPostData:[extraParams dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
//请求方法 put get post delete
[request setRequestMethod:aMethod];
if (type == 0)
{
// 同步请求 阻塞UI
[request startSynchronous];
}
else
{
//异步请求
[request startAsynchronous];
}
[request setTimeOutSeconds:120];
[request setNumberOfTimesToRetryOnTimeout:0];
当设置本地缓存时,Http请求 只能是get 方法如下
// 本地缓存
// [ASIHTTPRequest
// setDefaultCache:[ASIDownloadCache sharedCache]];
// [request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy ];
// [[ASIDownloadCache sharedCache ] setShouldRespectCacheControlHeaders:YES ];
// [request setSecondsToCache:60*60*24*30];//缓存30天
// 清除本地缓存
// [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];