文件通过visual studio下载,但不是通过.exe下载

时间:2017-09-28 13:06:07

标签: c# .net httpwebrequest

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

  

服务器提交了协议违规。第= ResponseHeader   细节= 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个网址下载文件。但我的.exe下载了前3个网址的文件。对于网址,D会抛出

  

服务器提交了协议违规。第= ResponseHeader   细节= CR必须后跟LF

更新2:我试图使用fiddler跟踪D网址。当我从浏览器运行D网址下载文件时,我得到了以下标题,文件已下载。另请注意,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

当我尝试使用D.exe网址下载文件时,我收到了以下标题

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

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

bin \ debug

Update3: dir /s /b

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

1 个答案:

答案 0 :(得分:3)

application configuration file需要与awk ' FNR==NR{ arr[$0]; next } { for(i in arr)if($0 ~ i){ print RS $0; next} } ' pattern_file RS='\>' FILE1 >file2 位于同一文件夹中。部署.exe时,您还应该将CLImport.exe部署到该文件夹​​。