File is downloading through visual studio but not

2019-06-20 17:16发布

问题:

Am getting the below error when I try to download the a .pdf file from a url through my .exe file.

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

but the same is getting downloaded when I try to debug the code from visual studio. I am totally lost, no clue of whats going on. Can somebody tell me what could be the problem

My App.config file

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

useUnsafeHeaderParsing="true" is the obvious fix that everyone is stating on internet unfortunately it is not working

Here is my webclient code

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;
    }
}

Update : My .exe was able to download most of the url's except few. Consider I have 4 url's :A,B,C and D. My visual studio was able to download files from all 4 urls' but my .exe download's file from first 3 url's. For url, D it throws

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

Update 2 : I was trying to trace D url using fiddler. When I ran the D url from browser to download the file, I got the below header and file was downloaded. Also note that D url is redirected to another url before downloading

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

When I tried to downlaod the file from D url using .exe I got the below header

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

For some reason the User-Agent is that the problem ?

Update3: dir /s /b of the 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

回答1:

The application configuration file needs to be in the same folder as the .exe. When you deploy CLImport.exe you should also deploy CLImport.exe.config to that folder.