I want to download a file from the internet (over https) using WinForms in C#
private void Button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
string user = "user";
string pwd = "pwd";
CredentialCache mycache = new CredentialCache();
if (!string.IsNullOrEmpty(url))
{
Thread thread = new Thread(() =>
{
//Uri uri = new Uri(url);
mycache.Add(new Uri(url), "Basic", new NetworkCredential(user, pwd));
client.Credentials = mycache;
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition","attachment;filename=\"" + "a.zip" + "\"");
byte[] data = client.DownloadData(url);
response.BinaryWrite(data);
response.End();
});
thread.Start();
}
}
When I start to download this line:
HttpResponse response = HttpContext.Current.Response;
threw an exception
'HttpContext.Current.Response' threw an exception of type 'System.NullReferenceException'`.
Can anyone help me solve this problem?