httpwebrequest超时错误c#



在编写代码的人离开公司后,我继承了一些代码。显然它从来没有起过作用,但我不知道为什么。

它正试图从台湾证券交易所网站下载一个包含外汇汇率的CSV文件。

URL是

http://rate.bot.com.tw/Pages/UIP004/Download0042.ashx?lang=en-美国&文件类型=1&afterOrNot=0&谁=SGD&日期1=20110401&日期2=20110430

如果我直接将该URL粘贴到Internet Explorer中,我会立即收到一个响应,询问我是否要打开或保存该文件,并且我可以毫无问题地打开和保存该文件。

当我运行C#代码获取文件时,大约90秒后我会收到错误消息"操作超时"。我试着把暂停时间改为3分钟,以防需要更长的时间,但仍然没有成功。我可以看到在本地创建的空文件,但没有任何内容被填充到其中

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

我尝试了两个不同的ContentType值——"text/plain"one_answers"application/octet stream"。两者都没有任何区别。

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

如有任何帮助,将不胜感激

设置以下属性:

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;

这应该行得通。

相关内容

  • 没有找到相关文章

最新更新