gpt4 book ai didi

ios - NSURLProtocol 和 post 上传进度

转载 作者:可可西里 更新时间:2023-11-01 04:04:08 25 4
gpt4 key购买 nike

我正在使用 NSURLProtocol 的子类来拦截所有 HTTP 调用并修改用户代理以及添加我的服务器所需的其他 http header 。

-(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;
}

然后我的协议(protocol)使用 NSURLConnection

- (void)startLoading
{
self.URLConnection = [NSURLConnection connectionWithRequest:self.innerRequest delegate:self];
}

然后我通过将调用转发到等效的 NSURLProtocolClient 方法来实现 NSURLConnection 中的所有委托(delegate)方法。这在一般情况下效果很好,但是当我将数据上传到服务器时,我使用 NSURLConnection 的代码不会被回调:

连接:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:

我理解这是因为我没有在 NSURLProtocol 中实现该方法,因为没有等效的 NSURLProtocolClient 方法可用于报告上传进度。

有人找到解决方法了吗?

最佳答案

将此行添加到您的代码中,使其成为 HTTP POST 请求:

[lInnerRequest setHTTPMethod:@"POST"];

关于ios - NSURLProtocol 和 post 上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9179761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com