gpt4 book ai didi

web-applications - WebClient.UploadFile失败,出现 "The request was aborted: The request was canceled."

转载 作者:行者123 更新时间:2023-12-04 08:31:36 25 4
gpt4 key购买 nike

我正在尝试使用WebClient.UploadFile将文件上传到HTTP URL。对于1M,2M等小文件,上传成功。但是对于12M之类的大文件,我遇到了以下异常:

The request was aborted: The request was canceled.

有人遇到过这个问题吗?您能否分享解决方案?

一些信息:
  • 服务器正在使用ASP.NET 3.5。
  • 我在上传中使用的HTTP方法是“PUT”。
  • 最佳答案

    这是smwikipedia的答案中提到的解决方案。我添加了禁用写流缓冲的功能,这可以帮助解决内存不足的异常。

    public class ExtendedWebClient : WebClient
    {
    public int Timeout { get; set; }
    public new bool AllowWriteStreamBuffering { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
    var request = base.GetWebRequest(address);
    if (request != null)
    {
    request.Timeout = Timeout;
    var httpRequest = request as HttpWebRequest;
    if (httpRequest != null)
    {
    httpRequest.AllowWriteStreamBuffering = AllowWriteStreamBuffering;
    }
    }
    return request;
    }

    public ExtendedWebClient()
    {
    Timeout = 100000; // the standard HTTP Request Timeout default
    }
    }

    用法:
    var webClient = new ExtendedWebClient();
    webClient.Timeout = Timeout.Infinite;
    webClient.AllowWriteStreamBuffering = false;
    webClient.UploadFile(url, filePath);

    关于web-applications - WebClient.UploadFile失败,出现 "The request was aborted: The request was canceled.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061469/

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