To jiangsheng(蒋晟.Net[MVP]) :
Uri LoginURL= new Uri("https://www.example.com/checklogon.asp");
string PostData = "username=Example&password=example";
似乎必须针对特定的网页内容来作处理,不具备通用性。
To lion_wing(凤之焚):
有没有全能的MIME Type?不知怎么拦截不到下载文件,连MIME Filter的DLL都没有加载。
CookieContainer CookieJar = new CookieContainer();
ServicePointManager.ServerCertificateValidationCallback +=
new RemoteCertificateValidationCallback(myCertificateValidation);
string TempFile=Path.GetTempFileName();
int buffSize = 4 * 1024; //4KB
byte[] buffer = new byte[buffSize];
using (FileStream fileStream = File.OpenWrite(TempFile))
{
while (true)
{
int count = DownloadStream.Read(buffer, 0, buffSize);
if (count == 0) break;
fileStream.Write(buffer, 0, count);
}
}
DownloadStream.Close();
DownloadResponse.Close();
File.Copy(TempFile, LocalFile, true);
}
void CreateHttpWebRequest(ref HttpWebRequest webRequest, Uri URL, ref CookieContainer cookie,
string UserAgent, string Method, string PostDataText)
{
webRequest
= (HttpWebRequest)WebRequest.Create(URL);
// set the HttpWebRequest objects cookie container
// if you have any cookies that you want to go with the request you can add them
// to the cookiecontainer. If you had made a previous request that returned any cookies
// that needed to be sent on subsequent request this will make sure that they are sent.
webRequest.CookieContainer = cookie;
// the default credentials are usually the Windows credentials (user name, password, and domain) of the user running the application
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.UserAgent = UserAgent;
webRequest.KeepAlive = true;
webRequest.Headers.Set("Pragma", "no-cache");
//set the request timeout to 5 min.
webRequest.Timeout = 300000;
webRequest.Method = Method;
// See what the Method is a POST
if ("POST" == Method)
{
// add the content type so we can handle form data
webRequest.ContentType = "application/x-www-form-urlencoded";
if (PostDataText.Length < 1) // we don't have any data to post
{
return;
}
// we need to store the data into a byte array
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(PostDataText);
webRequest.ContentLength = PostData.Length;
Stream tempStream = webRequest.GetRequestStream();
// write the data to be posted to the Request Stream
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();
}
return;
}