vb.net - 尝试将大量数据发送到 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
解决方案
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>
推荐阅读
- django - 匿名用户在使用 simplejwt 访问 rest-api 时遇到
- html - Django:无法解析余数:'{{' from '{{
- java - 为什么拥有 maven-resources-plugin 2.6 版的 mvn 插件会抱怨 maven-resources-plugin:2.6:testResources 失败?
- reactjs - 在 React Mapbox 上实现英制单位
- java - 为什么我不能使用与 java.file 不在同一活动中的按钮?
- python-3.x - vscode 测试错误
- javascript - 按日期对表行进行排序,比较种子文件中的 DateTime 和 Date.now()
- spring - 如何轻松配置编解码器?
- javascript - ajax 响应状态为 200 但触发错误
- excel - Excel VBA:遍历列以识别数据输入行