Search code examples
c#webclient

I want to download a file using C#. But I am getting error "The operation time out."


I am using WebClient to download a file from this URL https://nsearchives.nseindia.com/archives/nsccl/var/C_VAR1_16112023_1.DAT

But I am getting error

The operation time out.

string file_ = "https://nsearchives.nseindia.com/archives/nsccl/var/C_VAR1_16112023_1.DAT";
string path_ = "D:\Span4\Data\NSEVAR.Dat";

WebClient wc_ = new WebClient();
try
{
    wc_.Headers.Add(HttpRequestHeader.UserAgent, "Other");
    wc_.Headers.Add(HttpRequestHeader.Accept, "application/pdf");
    wc_.DownloadFile(file_, path_);
}
catch (Exception ex)
{
    MessageBox.Show("Error in VAR File Download " + ex.Message);
}

I have used the code. It works fine for file download from other websites. But the above mentioned URL is not working


Solution

  • Remove the line:

    wc_.Headers.Add(HttpRequestHeader.UserAgent, "Other");
    

    And try again. I think the server is not recognizing "Other" as a valid UserAgent and for this reason the request is timing out.