首页 > 解决方案 > 尝试将大量数据发送到 Web 服务时如何修复 404“未找到”错误

问题描述

我有一个 Web 服务,它允许客户端从数据库中获取查询并在客户端计算机上运行它们,将结果发布回 Web 服务。这些结果从 Datatable 解析为 xml,然后使用 WebClient.UploadValues 发送。只要查询/结果很小(仅返回几千行),我就可以发送和接收查询/结果,但是当查询返回数十万个结果时,xml 字符串变得非常大(特别是我正在使用的那个)是 70mb) 并且 Web 服务返回 404 Not Found。

我已经在 web.config 中为 Web 服务增加了 maxAllowedContentLength、maxRequestLength 和 executionTimeout。似乎客户端在收到该错误之前甚至都没有尝试将数据推送到 Web 服务。有没有办法告诉真正的问题是什么(xml字符串变量的限制?)?将数据发送到 Web 服务的代码如下。

    Using wc As New WebClient()
        Try
            Dim nvc As New NameValueCollection
            nvc.Add("params", myParameters)
            wc.UploadValues(URI, nvc)

        Catch ex As WebException
            eLog.WriteEntry("Application", My.Application.Info.AssemblyName + " - " + ex.Message)
        End Try
    End Using

我也试过这个方法:

    Try
        Dim client As New HttpClient()
        client.Timeout = TimeSpan.FromSeconds(300)
        Dim request As New HttpRequestMessage(HttpMethod.Post, URI)
        request.Content = New StringContent(myParameters)

        Dim response = client.SendAsync(request, HttpCompletionOption.ResponseContentRead)

    Catch ex As HttpRequestException
        eLog.WriteEntry("Application", My.Application.Info.AssemblyName + " - " + ex.Message)
    End Try

标签: vb.netweb-serviceshttpclientwebclient

解决方案


6 小时后,我随机决定重新排列 web.config 中的 maxAllowedContentLength 并解决了问题。我不知道“安全”标签的正确定位,但是这样放置解决了我的问题:

  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" maxRequestLength="2097152" executionTimeout="3600" />
  </system.web>
  <system.webServer>
    <defaultDocument>
      <files>
        <clear />
        <add value="Home.html" />
      </files>
    </defaultDocument>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

推荐阅读