我当前正在尝试构建一个应用程序,该应用程序会自动抓取网页以进行更新及其各自的下载链接和下载(如果需要)。目前,我正在尝试找到通过HttpClient下载ZIP文件的最佳方法。我访问的网页需要身份验证,因此我正在使用httpclient进行身份验证并创建一个cookiecontainer,使我能够下载所需的文件,但是,我一直在看所有API,无法弄清楚如何下载。我尝试了各种方法,但是还没有一种方法起作用。这是该方法的外观,但我仍然无法找到下载zip的好方法
var baseAddress = new Uri(url);
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
client.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
});
//Download Files
string[] download = getDownload().Split('/');
foreach (string link in download)
{
string path = @".\" + link + ".zip";
string dlink = url + link;
var uri = new Uri(dlink);