我使用NSURLProtocol的一个子类来拦截所有的HTTP调用,修改用户代理,并添加我的服务器所需的其他Http头。
-(id)initWithRequest:(NSURLRequest *)request
cachedResponse:(NSCachedURLResponse *)cachedResponse
client:(id <NSURLProtocolClient>)client
{
NSMutableURLRequest* lInnerRequest;
//************************************************
lInnerRequest = [request mutableCopy];
[lInnerRequest setValue:@"MyUserAgent" forHTTPHeaderField:@"User-Agent"];
//************************************************
self = [super initWithRequest:lInnerRequest
cachedResponse:cachedResponse
client:client];
//************************************************
if (self)
{
self.innerRequest = lInnerRequest;
}
//***********************************00*************
[lInnerRequest release];
//************************************************
return self;
}然后,我的协议使用NSURLConnection
- (void)startLoading
{
self.URLConnection = [NSURLConnection connectionWithRequest:self.innerRequest delegate:self];
}然后,我通过将调用转发到等效的NSURLProtocolClient方法来实现NSURLConnection中的所有委托方法。这通常工作得很好,但是当我将数据上传到服务器时,我使用NSURLConnection的代码不会被回调:
connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:
我理解这是因为我没有在NSURLProtocol中实现该方法,因为没有等效的NSURLProtocolClient方法可以用来报告上传进度。
有没有人找到解决这个问题的办法?
发布于 2012-02-08 00:22:26
将下面这一行添加到您的代码中,使其成为HTTP POST请求:
[lInnerRequest setHTTPMethod:@"POST"];发布于 2016-04-05 19:28:15
使用setProperty:forKey:inRequest:保存NSURLConnection和委托对象,然后使用propertyForKey:inRequest:加载自定义NSURLProtocol类中的NSURLConnection对象和委托对象,数据发送时使用NSURLConnection对象和委托调用委托方法
https://stackoverflow.com/questions/9179761
复制相似问题