【问题标题】:HttpWebRequest copy big file and get 404 not foundHttpWebRequest 复制大文件并得到 404 未找到
【发布时间】:2019-03-07 11:29:45
【问题描述】:

我复制没有问题的小文件。 当用户尝试上传大尺寸文件(我猜 > 60MB)时,我遇到了网络异常(404 Not Found)。

我很确定问题出在文件大小上。我在

遇到了异常
webRequest.GetResponse()

我需要修改服务器端吗? 任何建议都值得赞赏。

    public static bool UploadFile(IResult result, string pathFile, Stream stream)
    {
        // upload effettivo del file su DB
        HttpWebRequest webRequest = WebRequest.Create(ClientCommon.Properties.Settings.Default.FileServiceURI) as HttpWebRequest;
        HttpWebResponse response = null;
        Stream s = null;
        try
        {
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            webRequest.KeepAlive = true;

            using (WebRequestBuilder wrb = new WebRequestBuilder())
            {
                webRequest.ContentType = wrb.ContentType;

                wrb.AddTextPart("cdFile", pathFile);
                wrb.AddFilePart("file", stream);
                wrb.AddTextPart("destination", pathFile);


                if (wrb.GetContent(result, out s) == false)
                    return false;

                s.CopyTo(webRequest.GetRequestStream());
            }

            response = webRequest.GetResponse() as HttpWebResponse;

            return true;
        }
        catch (WebException exc)
        {
            result.SetError(exc.Message);
            return false;
        }
        finally
        {
            if (response != null)
                response.Close();

            if (s != null)
                // When the above code has ended, close the streams
                s.Close();
        }
    }

【问题讨论】:

  • 服务器是否有请求大小限制?
  • 您使用的是哪个应用程序WebAPIWebAPI Core 等等?
  • 尝试在httpRuntime 中设置属性maxRequestLength="2147483647"system.web 中的web.config
  • 尝试添加这个 => <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483647" /> </requestFiltering> </security><system.webServer> 并让我知道
  • 是的,很高兴听到,我将其添加为答案 :)

标签: c# http-status-code-404 httpwebrequest httpwebresponse


【解决方案1】:

尝试在您的web.config 中添加以下代码。

<system.web>
     <!-- Your other settings here -->
    <httpRuntime targetFramework="Your Framework" maxRequestLength="2147483647" />
</system.web>

<system.webServer>
    <!-- Your other settings here -->
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
      </requestFiltering>
    </security>
</system.webServer>

ma​​xRequestLength:属性maxRequestLength表示ASP.NET支持的最大文件上传大小。此限制可用于防止因用户向服务器发布大文件而导致的拒绝服务攻击。指定的大小以千字节为单位。默认值为 4096 KB (4 MB)。 Here你可以阅读更多关于maxRequestLength的信息。

ma​​xAllowedContentLength: 指定对 Web 服务器处理的请求的限制。 Here你可以阅读更多关于maxAllowedContentLength的信息。

通过上述代码,您可以接收长达 2GB 的文件。您可以根据自己的需要进行修改

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 2020-06-10
    • 1970-01-01
    • 2014-02-22
    • 2018-05-03
    • 1970-01-01
    • 2014-11-10
    • 2018-02-04
    相关资源
    最近更新 更多