使用 NSMutableURLRequest 通过 http post 发送文件时出现网络错误

发布于 2024-12-16 18:36:27 字数 708 浏览 2 评论 0原文

我在使用 setHTTPBodyStream 而不是带有 NSMutableURLRequest 的 setHTTPBody 时遇到问题。

我正在编写通过 http post 将大文件发送到服务器的代码。使用以下部分代码,一切正常:

NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
[request setHTTPBody:mydata];

如果我将其更改为:

NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
self.tmpInputStream = [NSInputStream inputStreamWithData:mydata];
[request setHTTPBodyStream: self.tmpInputStream];

那么我总是以网络错误结束:错误 - 操作无法完成。 (kCFErrorDomainCFNetwork 错误 303。)

最后的目标是直接从文件创建 inputStrem,以便能够发送大文件而无需将它们加载到内存中。

我是否错过了 setHTTPBodyStream 使用的某些内容?

感谢您的帮助。

问候。 塞巴斯蒂安.

I have a problem using setHTTPBodyStream instead of setHTTPBody with a NSMutableURLRequest.

I'm working on code to send large file to a server through http post. With the following portion code, everything works perfectly :

NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
[request setHTTPBody:mydata];

If I change it to :

NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
self.tmpInputStream = [NSInputStream inputStreamWithData:mydata];
[request setHTTPBodyStream: self.tmpInputStream];

Then I always end with a network error : Error - The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)

The goal is at the end to create inputStrem directly from the file to be able to send large file without to load them in memory.

Did I miss something with setHTTPBodyStream use ?

Thanks for your help.

Regards.
Sébastien.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-12-23 18:36:27

尝试设置 HTTP 方法(如果尚未设置)。我能够通过添加以下内容来修复“kCFErrorDomainCFNetwork error 303”错误:

[request setHTTPMethod:@"POST"];

Try setting the HTTP method (if you're not already). I was able to fix a "kCFErrorDomainCFNetwork error 303" error by adding:

[request setHTTPMethod:@"POST"];
爱,才寂寞 2024-12-23 18:36:27

当您使用setHTTPBody时,它会自动设置请求的Content-length标头,但是当您使用setHTTPBodyStream时,它不会,但是而是将传输编码设置为分块。当无法提前确定流的长度时,使用分块传输编码。 (请参阅分块传输编码。)

NSURLConnection 与 < code>setHTTPBodyStream,您可以手动设置Content-length,它将阻止chunkedTransfer-encoding(假设您知道长度提前):

NSString *contentLength = [NSString stringWithFormat:@"%d", [mydata length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-length"];

使用 NSURLSession 时,尝试与分块请求一起手动设置 Content-length 可能无法阻止其成为 分块要求。

When you use setHTTPBody, it automatically sets the Content-length header of the request, but when you use setHTTPBodyStream, it doesn't, but rather sets Transfer-encoding of chunked. A chunked transfer encoding is used when the length of the stream cannot be determined in advance. (See Chunked transfer encoding.)

When using NSURLConnection in conjunction with setHTTPBodyStream, you can manually set Content-length and it will prevent the Transfer-encoding of chunked (assuming you know the length in advance):

NSString *contentLength = [NSString stringWithFormat:@"%d", [mydata length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-length"];

When using NSURLSession, attempts to manually set Content-length in conjunction with a chunked request may not prevent it from being a chunked request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文