我收到此代码的异常错误,我在windows应用程序中创建了这个表单,代码是自动登录到站点并下载文件,并将其存储在一个文件夹中,同时调试我得到异常错误为“基础连接已关闭:发送时发生意外错误。”内部异常是“现有连接被远程主机c#强行关闭”
private void Form1_Load(object sender, EventArgs e)
{
try
{
string formUrl = "https://secure.gmail.com/account/doLogin.html?icmp=HO_LOGIN_BANNER"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("username={0}&password={1}", "username", "password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
string getUrl = "https://secure.gmail.com/account/summary.html";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
WebClient Client = new WebClient();
Client.DownloadFile("https://secure.gmail.com.com/account/callActivity/print.csv?lineNumber=16145838755&startDate=MNTH2014-04-24&fragment=true", @"C:\Downloads");
}
catch(Exception ex)
{
throw ex;
}
}
}
}