I have figured out(by inspecting both the website its-self and the response form the website when using my C# application) that the website in question does not use the form's nor cookies to do the secure connections so I am wondering if anyone knows of a way to potentially use the SSL certificate or the website headers(which by all accounts I think do have a cookie in them), to allow the log-in and then download of a certain file.
Could I get code examples or links to ways to use the headers to log-in to a site is what I am asking, I have all the necessary credentials just not the extensive knowledge to use them.
Thanks a bunch.
public void downloadStuff()
{
string formUrl = "https://secure.website.co.nz/extension/Login.aspx";
string formParams = string.Format("ctl00_main_Login1_UserName={0}&ctl00_main_Login1_Password={1}", "*********", "************");
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.website.co.nz/extension/Downloader.axd?x=stock-all";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
WebClient exeed = new WebClient();
exeed.DownloadFileAsync(new Uri(#"https://secure.website.co.nz/extension/Downloader.axd?x=stock-all"), "test.txt");
pageSource = sr.ReadToEnd();
}
}
Note: This question was also put up because if what I have done is correct, I would not know how to use a cookie to download a file. Potentially example code of how to capture and use a cookie and or a header would be the best option.
Thanks, This was an edit!
Related
I created a c# console application to log into a website with Selenium and it works great. Now I want to add this feature to an existing .Net Core application.
So far I have the following:
string formUrl = "https://cap.mcmaster.ca/mcauth/login.jsp?app_id=1505&app_name=Avenue";
string formParams = string.Format("user_id={0}&pin={1}", "user", "pass"); //In my program I have the correct credentials
string cookieHeader;
var cookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.CookieContainer = cookies;
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://avenue.cllmcmaster.ca/d2l/home";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(resp.cookies);
getRequest.Headers.Add("Cookie", cookieHeader);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
For some reason, in the above code Cookies is underlined in "resp.Cookies" as an error. (CS1061 'WebResponse' does not contain a definition for 'Cookies' and no accessible extension method 'Cookies' accepting a first argument of type 'WebResponse' could be found)
I tried to folow the posts here: Login to website, via C# but I can't seem to get it to work.
The reason you are getting a compile time error is because the WebResponse class does not have a Cookies property.
From .NET Docs
The WebResponse class is the abstract base class from which
protocol-specific response classes are derived.
Instead, take a look at the HttpWebResponse class. This class provides a HTTP specific implementation of WebResponse.
You can cast the WebResponse to a HttpWebResponse:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpWebResponse.Cookies Property
Gets or sets the cookies that are associated with this response.
Documentation
HttpWebResponse.Cookies
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to login to this website using C#: Here is my attempt but its sending me to first page. Not returning me the next page, that should be visible after login, please help me to resolve this:
string formParams =
string.Format("mail={0}&password={1}", store#admin.com", "admin");
string cookieHeader;
WebRequest req = WebRequest.Create("http://muslimgowns.com/dashboard/login/public_login");
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"];
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string pageSource = sr.ReadToEnd();
File.AppendAllText("first.txt", pageSource);
}
string pageSource1;
string getUrl = "http://muslimgowns.com/dashboard/home";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource1 = sr.ReadToEnd();
File.AppendAllText("second.txt", pageSource1);
}
}
It seems like the first GET request to public_login returns an amount of cookies, and the POST request with credentials afterwards must be sent to login_access instead of public_login
The use of an HttpWebRequest instead of WebRequest and by setting the cookie container of it helps, and actually the server responds with HTTP 302 Redirect to the POST request and the HttpWebRequest automatically follows this redirection and downloads the dashboard.
Always use an http tracing tool like Fiddler or Wireshark or Network Monitor or developer tools of your browser to see what is received (cookies, headers etc.) and what is sent back. That's how I got all this.
Here is the fix:
string formParams = string.Format("mail={0}&password={1}", "store#admin.com", "admin");
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/public_login");
req.CookieContainer = cookieContainer;
req.GetResponse(); // This is just to get the initial cookies returned by the public_login
req = WebRequest.CreateHttp("http://muslimgowns.com/dashboard/login/login_access");
req.CookieContainer = cookieContainer; // Set the cookie container which contains the cookies returned by the public_login
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();
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string pageSource = sr.ReadToEnd();
File.AppendAllText("first.txt", pageSource); // Dashboard is returned.
}
I am trying to get my c# console application to check if the credentials supplied are correct. But when I try it the web page gives a error:
<div id="login_error"> <strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.<br />
This is my code:
static bool SendRequest(string Username, string Password, string URL) {
string formUrl = URL;
string formParams = string.Format("log={0}&pwd={1}&wp-submit={2}&redirect_to={3}&testcookie={4}", Username, Password, "Log In", "http://localhost/wp-admin/", "1");
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;
((HttpWebRequest)req).CookieContainer = new CookieContainer();
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
Console.Write(pageSource);
return true;
}
I think the problem is that the cookies are not working but I have no clue how to fix this.
All help is appreciated!
You're not setting a CookieContainer to your HttpWebRequest, thus the default is set to null which means the client won't accept cookies.
CookieContainer from MSDN
The CookieContainer property provides an instance of the
CookieContainer class that contains the cookies associated with this
request.
CookieContainer is null by default. You must assign a
CookieContainer object to the property to have cookies returned in the
Cookies property of the HttpWebResponse returned by the GetResponse
method.
You must set a new CookieContainer before getting a response from the server.
req.ContentLength = bytes.Length;
((HttpWebRequest)req).CookieContainer = new CookieContainer();
// Rest of the code here..
The question is clear I hope.
I want log in to DeviantArt through my application and I found stuff but it just doesn't seem to work...
Here's my code(running in a thread):
//===========================================
// Login
//===========================================
string formUrl = "https://www.deviantart.com/users/login";
string formParams = string.Format("login-username={0}&login-password={1}", "mai username", "mai password");
string cookieHeader;
System.Net.WebRequest req = System.Net.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);
}
//=========Send Cookie=============
System.Net.WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
string pageSource;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
string getUrl = "http://www.deviantart.com/users/loggedin";
System.Net.WebRequest getRequest = System.Net.WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookieHeader);
System.Net.WebResponse getResponse = getRequest.GetResponse();
I get a 403 error at the last line where I'm trying to get response. Some information about DeviantArt: 1: You can access pages etc. but you can't comment or anything. 2: When I tried to check if deviantart.com existed it gave me a 403 no permission error so that's why I'm trying to log in. When I write pageSource to a textfile I get this. What do I do wrong and how do I fix it? because I really have no clue why this isn't working...
PS: Alternate ways to do this are also welcome except Selenium
I need to login to a website a download the source code from various pages when logged in. I am able to do this quite easily when using the Windows Forms WebBrowser class, however this is not appropiate and I need to be able to do this with WebRequest or one of the others. Unfortunately it doesn't like how I am handling the cookies.
I am using the following code and get the following response: {"w_id":"LoginForm2_6","message":"Please enable cookies in your browser so you can log in.","success":0,"showLink":false}
string url2 = "%2Fapp%2Futils%2Flogin_form%2Fredirect%2Fhome";
string login = "username";
string password = "password";
string w_id = "LoginForm2_6";
string loginurl = "http://loginurl.com";
string cookieHeader;
WebRequest req = WebRequest.Create(loginurl);
req.Proxy = WebRequest.DefaultWebProxy;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
req.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
req.Method = "POST";
string postData = string.Format("w_id={2}&login={0}&password={1}&url2={3}", login, password, w_id, url2);
byte[] bytes = Encoding.ASCII.GetBytes(postData);
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 = "";
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
richTextBox1.Text = pageSource;
If anyone could tell me where I'm going wrong, it would be greatly appreciated.
Also, to let you know, if I use the following with the webbrowser class, it works in fine:
b.Navigate(fullurl, "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
I know this is old to reply, but the user Matthew Brindley answered similar question with a completely working example. The question is about accessing to the
source code of a website that requires user login previously. All done from a C# application using WebRequest and WebResponse