C#https登录和下载文件
我已成功连接到登录页面,但是,我不确定如何登录并获取登录后面的文件。 下面是我用来连接的代码。
private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) { return true; } public static void Processing() { string url = "https://app/templat"; HttpWebRequest request; HttpWebResponse response; CookieContainer cookies; ServicePointManager.ServerCertificateValidationCallback = System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, cert, chain, errors) => cert.Subject.Contains("YourServerName")); ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(bypassAllCertificateStuff); try { request = (HttpWebRequest)WebRequest.Create(url); request.AllowAutoRedirect = false; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = new CookieContainer(); response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { cookies = request.CookieContainer; request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; String postData = "j_login=user&j_password=user&submit=Send"; byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(postData); request.ContentLength = data.Length; //Stream stream = request.GetRequestStream(); //stream.Write(data, 0, data.Length); request.CookieContainer = cookies; //stream.Close(); StreamReader sr = new StreamReader(response.GetResponseStream()); string tmp = sr.ReadToEnd().Trim(); //response = (HttpWebResponse)request.GetResponse(); //WebClient wbClient = new WebClient(); //wbClient.DownloadFile("https://app/template/simple%2Screen.vm", @"C:\test.xls"); response.Close(); } else { Console.WriteLine("Client was unable to connect!"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
我很肯定下载不起作用,我确定String postData
不执行它的意图。
以下是网站登录的代码