Karen Wickham
Karen Wickham

Reputation: 33

httpwebrequest timeout error c#

I've inherited some code after the guy who wrote it left the company. Apparently it has never worked but I don't know why.

It's trying to download a CSV file containing FX rates from the Taiwan stock exchange website.

The URL is

http://rate.bot.com.tw/Pages/UIP004/Download0042.ashx?lang=en-US&fileType=1&afterOrNot=0&whom=SGD&date1=20110401&date2=20110430

If I paste that URL directly into Internet Explorer, I get a response straight away asking me if I want to open or save the file, and I can open and save the file with no problems.

When I run the C# code to get the file I get the error message "The operation has timed out" after about 90 seconds. I tried changing the timeout period to 3 minutes in case it needed longer, but still no luck. I can see the empty file being created locally but nothing gets populated into it.

private static void GetVendorFiles(String sUrl, String sFileNameToWrite, String sProxy)
{
    DateTime dtStart = System.DateTime.Now;

    using (CertWebClient client = new CertWebClient())

    try
    {
        WebProxy proxy = new WebProxy(sProxy);
        proxy.Credentials = CredentialCache.DefaultCredentials;
        WebRequest.DefaultWebProxy = proxy;

        //Override the default policy of System.Net not accepting self-signed certificates
        System.Net.ServicePointManager.ServerCertificateValidationCallback
         = CertificatePolicy.ValidateSSLCertificate;

        //Download file and write to network drive
        client.DownloadFile(sUrl, sFileNameToWrite);

        proxy = null;

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message); 
        TraceHelper.WriteLine("PathHelper.DownLoadFiles:" + ex.Message);
        throw;
    }
}

I tried two different ContentType values - "text/plain" and "application/octet-stream". Neither made any difference.

class CertWebClient : WebClient      
{
    protected override WebRequest GetWebRequest(Uri address) 
    {

        try
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
            request.ContentType = "application/octet-stream";
            return request;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message); 
            TraceHelper.WriteLine("Taiwan FX Rates Web Certificate Error:" + ex.Message);
            throw;
        }
    }

    public static void Getcert(string certLocation,string sPassword)
    {

        if (certLocation == "")
        {
            throw new ArgumentException ("Null certificate location passed to CertWebClient");
        }

        if (sPassword == "")
        {
            throw new ArgumentException("Null password passed to CertWebClient");
        }
        certificateLocation = certLocation;
        certPassword = sPassword;   
    }

    private static string certificateLocation;
    private static string certPassword;
} 

Any help would be appreciated

Upvotes: 1

Views: 1110

Answers (1)

Georgi-it
Georgi-it

Reputation: 3686

Set the following properties:

request.Method = "GET";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"
request.KeepAlive = true;
request.Timeout = Timeout.Infinite;

This should work.

Upvotes: 1

Related Questions