C # https login and upload file
I have successfully connected to the login page, however, I am not sure how to login and grab the file that is behind the login. Below is the code I am using to connect.
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());
}
}
I'm pretty sure the download is not working and I'm pretty sure it is String postData
not doing what it intends.
Below is the login code
<pre>
<form name=\"loginform\" method=\"post\" action=\"j_security_check\" onSubmit=\"javascript:fixFields();\">
<br>
<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"80%\">
<tr>
<td colspan=\"2\" align=\"center\" nowrap=\"nowrap\">
<div id=\"bannerDiv\" class=\"groupingBorder\" style=\"visibility:hidden;position:relative;background-color:#FFFFFF; overflow:auto;\">
</div>
</td>
</tr>
<tr>
<td class=\"contentrtanbld\" nowrap width=\"50%\">Name:</td>
<td class=\"contentltan\" nowrap width=\"50%\">
<input type=\"text\" name=\"j_username\" id=\"j_username\" value=\"\" class=\"authGroupWidth\" size=\"20\"></td>
</tr>
<tr>
<td class=\"contentrtanbld\" nowrap>Password:</td>
<td class=\"contentltan\" nowrap>
<input type=\"password\" name=\"j_password\" id=\"j_password\" value=\"\" class=\"authGroupWidth\" size=\"20\"></td>
</tr>
<tr>
</pre>
And the file I want to download is via this link
https://app/template/simple%2Screen.vm
I can connect to the web page, but I'm not sure how to login and download the file.
Please refer to the code update. It's not logged in yet, and I'm not sure why.
string url = "https://mgr/app";
HttpWebRequest request;
HttpWebResponse response;
CookieContainer cookies = new CookieContainer();
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
{
string cookieHeader;
string formParams = string.Format("j_login={0}&j_password={1}", "user", "user");
request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
request.ContentLength = bytes.Length;
using (Stream os = request.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = request.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
string BehinPath = "https://mgr/app/action/store.VivolAction/eventsubmit_dopreparevivollist/ignored";
WebRequest getRequest = WebRequest.Create(BehinPath);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
This is new code, there is 1 cookie, however, when I try the first post, it never logs in.
There are the following problems in your code that I see:
- Doesn't handle cookie container correctly. The CookieContainer must be initialized and then passed to your HttpWebRequest, not the other way around.
- Doesn't clear disposable objects. Failure to dispose of an object can cause the object to hang for quite some time before the garbage collector catches it.
- Does not take into account the effect of the form. Your form action will result in the submission to a different location.
- It is unnecessary to perform the first operation as a POST. Use GET instead.
- Does not set the referent when performing a POST operation.
Try using the following code:
Uri url = new Uri("http://app/templat");
HttpWebRequest request = null;
// Uncomment the line below only if you need to accept an invalid certificate, i.e. a self-signed cert for testing.
// ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
CookieContainer cookieJar = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieJar;
request.Method = "GET";
HttpStatusCode responseStatus;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
responseStatus = response.StatusCode;
url = request.Address;
}
if (responseStatus == HttpStatusCode.OK)
{
UriBuilder urlBuilder = new UriBuilder(url);
urlBuilder.Path = urlBuilder.Path.Remove(urlBuilder.Path.LastIndexOf('/')) + "/j_security_check";
request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
request.Referer = url.ToString();
request.CookieContainer = cookieJar;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (Stream requestStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
{
string postData = "j_username=user&j_password=user&submit=Send";
requestWriter.Write(postData);
}
string responseContent = null;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader responseReader = new StreamReader(responseStream))
{
responseContent = responseReader.ReadToEnd();
}
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine("Client was unable to connect!");
}
source to share
First of all, you need to initiate the cookie container before the first request:
CookieContainer cookies = new CookieContainer();
then you need to pass it on every request (now you just instantiate it, thereby losing all cookies):
request.CookieContainer = cookies;
the response you receive will fill the cookie container with the necessary cookies.
Also, as I see you are already doing, you need to execute a series of requests / responses on the website and track the cookies. Use a tool like Fiddler to find out exactly how you need to frame your POST strings to log into your website correctly.
source to share