我需要一些来自门户的图像,并且只有在我登录到门户时才能访问它们。
我需要用 C# 程序来做。我不知道用户名字段和密码字段是什么,因为它们使用 POST 方法。登录后,我想输入一些包含我想要的图像的 URL。
我应该怎么办?
对于登录,我正在使用:
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(@"http://mysite.com");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "UsernameFieldName=Something";
postData += "&PasswordFieldName=SomethingElse";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
然后下载另一个页面中的图像,我使用:
using (var client = new WebClient())
{
string FileName = @"image.jpg";
client.DownloadFile("http://mysite.com/Image?imgCode=12345", FileName);
}