NSURLProtocal子类的使用

//

//  CustomURLProtocol.m

//  NSURLProtocolExample

//

//  Created by lujb on 15/6/15.

//  Copyright (c) 2015年 lujb. All rights reserved.

//

#import "CustomURLProtocol.h"

static NSString * const URLProtocolHandledKey = @"URLProtocolHandledKey";

@interface CustomURLProtocol ()<NSURLConnectionDelegate>

@property (nonatomic, strong) NSURLConnection *connection;

@end

@implementation CustomURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request

{

//只处理http和https请求

NSString *scheme = [[request URL] scheme];

if ( ([scheme caseInsensitiveCompare:@"http"] == NSOrderedSame ||

[scheme caseInsensitiveCompare:@"https"] == NSOrderedSame))

{

//看看是否已经处理过了,防止无限循环

if ([NSURLProtocol propertyForKey:URLProtocolHandledKey inRequest:request]) {

return NO;

}

return YES;

}

return NO;

}

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {

NSMutableURLRequest *mutableReqeust = [request mutableCopy];

mutableReqeust = [self redirectHostInRequset:mutableReqeust];

return [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];

}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b

{

return [super requestIsCacheEquivalent:a toRequest:b];

}

- (void)startLoading

{

/* 如果想直接返回缓存的结果,构建一个NSURLResponse对象

if (cachedResponse) {

NSData *data = cachedResponse.data; //缓存的数据

NSString *mimeType = cachedResponse.mimeType;

NSString *encoding = cachedResponse.encoding;

NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.URL

MIMEType:mimeType

expectedContentLength:data.length

textEncodingName:encoding];

[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

[self.client URLProtocol:self didLoadData:data];

[self.client URLProtocolDidFinishLoading:self];

*/

NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];

//打标签,防止无限循环

[NSURLProtocol setProperty:@YES forKey:URLProtocolHandledKey inRequest:mutableReqeust];

self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];

}

- (void)stopLoading

{

[self.connection cancel];

}

#pragma mark - NSURLConnectionDelegate

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[self.client URLProtocol:self didLoadData:data];

}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

[self.client URLProtocolDidFinishLoading:self];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

[self.client URLProtocol:self didFailWithError:error];

}

#pragma mark -- private

+(NSMutableURLRequest*)redirectHostInRequset:(NSMutableURLRequest*)request

{

if ([request.URL host].length == 0) {

return request;

}

NSString *originUrlString = [request.URL absoluteString];

NSString *originHostString = [request.URL host];

NSRange hostRange = [originUrlString rangeOfString:originHostString];

if (hostRange.location == NSNotFound) {

return request;

}

//定向到bing搜索主页

NSString *ip = @"cn.bing.com";

// 替换host

NSString *urlString = [originUrlString stringByReplacingCharactersInRange:hostRange withString:ip];

NSURL *url = [NSURL URLWithString:urlString];

request.URL = url;

return request;

}

@end

时间: 2024-08-26 17:25:29

NSURLProtocal子类的使用的相关文章

【转】父类子类指针相互转换问题

1.当自己的类指针指向自己类的对象时,无论调用的是虚函数还是实函数,其调用的都是自己的: 2.当指向父类对象的父类指针被强制转换成子类指针时候,子类指针调用函数时,只有非重写函数是自己的,虚函数是父类的: 3.当指向子类对象的子类指针被强制转换成父类指针的时候,也就是父类指针指向子类对象,此时,父类指针调用的虚函数都是子类的,而非虚函数都是自己的. 将上面三句话总结成一句话就是:当父类子类有同名非虚函数的时候,调用的是转换后的指针类型的函数: 当父类子类有同名虚函数的时候呢,调用的是指针转换前指

在子类中,一定要访问父类的有参方法?

abstract class Person{ private int age; private String name; public Person(int age,String name){ this.age=age; this.name=name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name

继承中子类构造函数相关问题

Day08_SHJavaTraing_4-13-2017 1.为什么任何一个类(不包含Object)的构造函数中都需要一个super() 语句? 因为除了Object类以外,所有类都会继承一个父类:继承父类,那么子类实例化时就需要给父类中的成员变量显示赋值,就需要用到父类中的构造函数. 2.如果父类中没有无参构造函数,子类如何实例化? super()表示调用父类无参构造函数:如果父类中没有无参构造函数,就会报错. 如何解决这个问题呢? 方法①在父类中添加一个无参构造函数 方法②在子类的构造函数中

C++ 中超类化和子类化常用API

在windows平台上,使用C++实现子类化和超类化常用的API并不多,由于这些API函数的详解和使用方法,网上一大把.本文仅作为笔记,简单的记录一下. 子类化:SetWindowLong,GetWindowLong,CallWindowProc,FindWindowEx 超类化:GetClassInfoEx,RegisterClassEx,UnRegisterClass 以上函数在代码中的使用见<C++ 中超类化和子类化> VC中基于SDK编程的窗口子类化 VC中基于SDK编程的窗口子类化的

父类和子类之间的转换

结论: 1.父类(基本类)转子类(扩展类),报错 2.子类(扩展类)转父类(基本类),成功 实验如下: 父类(基本类) public class Person { private String name; private String doc; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDoc() { return

python高级编程之(类级):子类内建类型

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一(请访问:https://www.python.org/download/releases/2.2.3/descintro(可能已经不存在了))-这使内建类型的子类化成为可能,并且添加一个新内建类型object #用于所有内建类的公共祖先 #展示一个名为distinctdict类的代码,与平常的dic

关于继承时子类重写父类方法和覆盖父类变量的若干问题

假设,子类重载父类的方法,并将子类的成员覆盖. 创建子类对象实例,将其上转型成父类. 例子1 public class Parent { public void init() { System.out.println("1 init parent"); this.demo(); } public void demo() { System.out.println("2 demo parent"); } } public class Son extends Parent

java中子类继承

[[email protected] java]# vim Ostrich.java //注意文件名必须是这个,因为下面代码中只有Ostrich是public修饰符.我们要明白public的含义 class Bird { public void Fly() { System.out.println("I am bird I can fly"); } } public class Ostrich extends Bird { public void Fly() { System.out.

[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用

一.事件监听 对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1.对每一个button设置事件监听器button.setOnClickListener(View.OnclickListener  listener);此种方法当button按钮较多时代码显得多.乱.不够简洁明了. 2.在Activity中实现接口View.OnclickListener,然后重写void onClick(View v)方法,在方法中通过switch(v.getId())予以区分不同