【问题标题】:File is downloading through visual studio but not through .exe文件正在通过 Visual Studio 下载,但不是通过 .exe
【发布时间】:2023-03-29 00:36:01
【问题描述】:

当我尝试通过我的 .exe 文件从 url 下载 .pdf 文件时,出现以下错误。

服务器违反了协议。部分=响应头 Detail=CR后面必须跟LF

但当我尝试从 Visual Studio 调试代码时,也会下载相同的内容。我完全迷路了,不知道发生了什么。谁能告诉我可能是什么问题

我的App.config 文件

<?xml version="1.0"?>
<configuration>
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
  </system.net>
</configuration>

useUnsafeHeaderParsing="true" 是每个人都在互联网上声明的明显修复,不幸的是它不起作用

这是我的网络客户端代码

public class CookieAwareWebClient : WebClient {
    private CookieContainer cc = new CookieContainer();
    private string lastPage;

    protected override WebRequest GetWebRequest(Uri address) {
        if (address.Scheme == Uri.UriSchemeHttps) {
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
            // allows for validation of SSL conversations
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        }
        WebRequest R = base.GetWebRequest(address);
        if (R is HttpWebRequest) {
            HttpWebRequest wr = (HttpWebRequest)R;
            wr.CookieContainer = cc;
            if (lastPage != null) {
                wr.Referer = lastPage;
            }
        }
        lastPage = address.ToString();
        return R;
    }
}

更新:我的.exe 能够下载除少数之外的大部分网址。考虑我有 4 个网址:A,B,C and D。我的视觉工作室能够从所有 4 个 url 下载文件,但我的 .exe 从前 3 个 url 下载文件。对于 url,D 它会抛出

服务器违反了协议。部分=响应头 Detail=CR后面必须跟LF

更新 2: 我试图使用提琴手追踪 D 网址。当我从浏览器运行D url 来下载文件时,我得到了下面的标题并且文件已下载。另请注意,D 网址在下载之前会重定向到另一个网址

CONNECT www.loim.com:443 HTTP/1.1
Host: www.loim.com:443
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36

当我尝试使用.exeD url 下载文件时,我得到了以下标题

CONNECT www.loim.com:443 HTTP/1.1
Host: www.loim.com
Connection: Keep-Alive

由于某种原因,User-Agent 是问题所在吗?

Update3: dir /s /b 的 bin\debug

C:\Pradeep\TFS\proj\bin\Debug\app.publish
C:\Pradeep\TFS\proj\bin\Debug\CLImport.application
C:\Pradeep\TFS\proj\bin\Debug\CLImport.exe
C:\Pradeep\TFS\proj\bin\Debug\CLImport.exe.config
C:\Pradeep\TFS\proj\bin\Debug\CLImport.exe.manifest
C:\Pradeep\TFS\proj\bin\Debug\CLImport.pdb
C:\Pradeep\TFS\proj\bin\Debug\CLImport.vshost.application
C:\Pradeep\TFS\proj\bin\Debug\CLImport.vshost.exe
C:\Pradeep\TFS\proj\bin\Debug\CLImport.vshost.exe.config
C:\Pradeep\TFS\proj\bin\Debug\CLImport.vshost.exe.manifest
C:\Pradeep\TFS\proj\bin\Debug\FED.Business.Collection.dll
C:\Pradeep\TFS\proj\bin\Debug\FED.Business.Collection.pdb
C:\Pradeep\TFS\proj\bin\Debug\FED.Data.Collection.dll
C:\Pradeep\TFS\proj\bin\Debug\FED.Data.Collection.pdb
C:\Pradeep\TFS\proj\bin\Debug\FED.DataSource.Utilities.dll
C:\Pradeep\TFS\proj\bin\Debug\FED.DataSource.Utilities.pdb
C:\Pradeep\TFS\proj\bin\Debug\GemBox.Spreadsheet.dll
C:\Pradeep\TFS\proj\bin\Debug\ICSharpCode.SharpZipLib.dll
C:\Pradeep\TFS\proj\bin\Debug\Ignored
C:\Pradeep\TFS\proj\bin\Debug\itextsharp.dll
C:\Pradeep\TFS\proj\bin\Debug\Microsoft.Exchange.WebServices.dll
C:\Pradeep\TFS\proj\bin\Debug\Processed
C:\Pradeep\TFS\proj\bin\Debug\tt.text
C:\Pradeep\TFS\proj\bin\Debug\app.publish\CLImport.exe

【问题讨论】:

  • 您在使用不同的环境吗?问题似乎出在newline。看看这个艺术:NewLine 特别是在 CR+LFLF+CR
  • @p__d - 没有尝试在相同的环境中下载它,这是最奇怪的部分。 “问题似乎在换行”你能扩展吗,我没明白
  • 我在这个领域受过教育,这与我写的 newline 不完全一样。我可以打另外三个路径。首先,您是否也有web.config 而不是app.config?其次,尝试使用release mode 运行.exe 文件。第三,下载一些sniffer 并查找response header 中的内容。
  • @jessehouwing - 我的问题是,如果代码有问题,那么我如何在 Visual Studio 中下载文件
  • @jessehouwing - 更新了..这就是你要的吗?我是 sql 人,我的新公司让我在 c# 中工作,所以请原谅我问了一些蹩脚的问题

标签: c# .net httpwebrequest


【解决方案1】:

application configuration file 需要与.exe 位于同一文件夹中。当您部署 CLImport.exe 时,您还应该将 CLImport.exe.config 部署到该文件夹​​。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多