我需要下载一些大文件,所以我需要比默认1分钟更长的DownloadFile()超时。
问题是我的解决方案不会增加超时。它停留在1分钟。
我创建了这个类:
public class MyWebClient : WebClient
{
private int timeout;
public int Timeout
{
get
{
return timeout;
}
set
{
timeout = value;
}
}
public MyWebClient()
{
this.timeout = 1200000;
}
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = this.timeout;
return w;
}
}
这样称呼:
using (MyWebClient client = new MyWebClient())
{
client.Timeout = 1200000;
client.DownloadFile(new Uri(fileUrl), localFile);
}
有什么想法吗?
谢谢!
答案 0 :(得分:0)
更改您的代码如下
template<typename Map>
struct map_insert_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
public:
map_insert_iterator(Map& m) : map(m)
{
}
map_insert_iterator<Map>& operator = (const typename Map::value_type& value)
{
map.insert_or_assign(value.first, value.second);
return *this;
}
map_insert_iterator<Map>& operator * () { return *this; }
map_insert_iterator<Map>& operator ++ () { return *this; }
map_insert_iterator<Map>& operator ++ (int) { return *this; }
private:
Map& map;
};
template<typename Map>
map_insert_iterator<Map> map_inserter(Map& m)
{
return map_insert_iterator<Map>(m);
}
如你所说的那样打电话。
更新: - 其他明智的用途如下
private class MyWebClient : WebClient
{
public int Timeout { get; set; }
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = Timeout;
((HttpWebRequest) w).ReadWriteTimeout = Timeout;
return w;
}
}
答案 1 :(得分:0)
在客户请求中尝试此代码
using (MyWebClient client = new MyWebClient())
{
client.Timeout = 1200000;
byte[] data = client.DownloadData(url);
File.WriteAllBytes(localFile, data);
}