I have been trying to get it working for hours with little success. From source after first request I can see that login was successful, but using cookies to access other keeps asking for login info.
Here is site which I'm trying to access: http://gpro.net/
And here is the code I'm using:
string formUrl = "http://gpro.net/pl/Login.asp?langCode=pl&Redirect=gpro.asp"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = "textLogin=user&textPassword=pass&Logon=Login&LogonFake=Sign+in";
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create(formUrl);
requestLogin.Method = "POST";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.ContentLength = formParams.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(formParams);
stOut.Close();
HttpWebResponse responseLogin = (HttpWebResponse)requestLogin.GetResponse();
StreamReader stIn = new StreamReader(responseLogin.GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
sb.Append(strResponse);
sb.AppendLine();
sb.AppendLine();
sb.AppendLine();
//Add cookies to CookieJar (Cookie Container)
foreach (Cookie cookie in responseLogin.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
sb.AppendLine(cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString());
}
//Grab the cookie we just got back for this specifc page
string cookies = cookieJar.GetCookieHeader(requestLogin.RequestUri);
//put it back in the cookie container for the whole server
cookieJar.SetCookies(new Uri("http://www.gpro.net/"), cookies);
string testResults = string.Empty;
HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create("http://www.gpro.net/pl/RaceAnalysis.asp");
runTest.Referer = "http://gpro.net/pl/gpro.asp";
runTest.CookieContainer = cookieJar;
//runTest.Method = "POST";
//runTest.ContentType = "application/x-www-form-urlencoded";
//StreamWriter stOut2 = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
//stOut2.Write(formParams);
//stOut2.Close();
StreamReader stIn2 = new StreamReader(runTest.GetResponse().GetResponseStream());
testResults = stIn2.ReadToEnd();
stIn2.Close();
sb.Append(testResults);
e.Result = sb.ToString();
I found out what was wrong with code above and every similar one posted in stack overflow (I have tried all marked as working). When I was logging with browser redirection worked fine, with but using above method my request was redirected to same address, but without www prefix. That resulted with two different cookies collections. I removed www prefix from every request which solved login problem.
Related
Pretty much a noob here, having issues here with a Post Request in C#. I am returning the server response to webBrowser Control, and no matter what I try, I just get the login page returned with no form values posted. Any help here would be greatly appreciated!
Login Page: "https://www.carrier411.com/".
URL example that I want to return:(You have to be logged in to view)
"https://www.carrier411.com/manager/companydetail.cfm?docket=MC1114638"
var username = "******"; var password = "******";
string url = "https://www.carrier411.com/manager/companydetail.cfm?docket=MC1073504";
string postData = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + passWord));
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.ContentType = "text/html";
httpRequest.Headers.Add("Authorization", "Basic " + postData);
httpRequest.Accept = #"text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{streamWriter.Write(postData);}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
var streamReader = new StreamReader(httpResponse.GetResponseStream());
string result = streamReader.ReadToEnd().ToString();
webBrowser1.DocumentText = result;
http://www.conquerclub.com/game.php?game=13025037
Please does anyone know how I can programatically log onto this address and then get all the html of the page back as a string Using something like HttpWebRequest/HttpWebResponse.
username - "testuser1"
password - "testing"
(I've created an account on the site with these credentials so it will actually logon)
So far I've got this
private void toolStripButton1_Click(object sender, EventArgs e)
{
var request = WebRequest.Create("http://www.conquerclub.com/game.php?game=13025037");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
richTextBox1.Text = reader.ReadToEnd();
}
This automatically re-directs and returns the html of the login page. How do I pass the username and password to login automatically so it retrieves the game page instead?
I know there are quite a few similar questions on this and I've spent several hours trying different things but can't get anything to work.
Edit:-
Looked into this further and this site uses cookies for logging in.
Tried this code but is still just returning the login page.
private void toolStripButton1_Click(object sender, EventArgs e)
{
string loginUri = "http://www.conquerclub.com/login.php";
string username = "testuser1";
string password = "testing";
string reqString = "username=" + username + "&password=" + password;
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
CookieContainer cc = new CookieContainer();
var request = (HttpWebRequest)WebRequest.Create(loginUri);
request.Proxy = null;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
using (Stream s = request.GetRequestStream())
s.Write(requestData, 0, requestData.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.Cookies != null)
{
foreach (Cookie c in response.Cookies)
Console.WriteLine(c.Name + " = " + c.Value);
}
}
string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";
HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
newrequest.Proxy = null;
newrequest.CookieContainer = cc;
using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
using (Stream resSteam = newresponse.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
richTextBox1.Text = sr.ReadToEnd();
}
Have found that the code at the bottom to log into the game page can be made to work by first logging in manually while using fiddler on firefox then copy and pasting the cookies and hard coding them into newrequest like this.
string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";
HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
newrequest.Proxy = null;
newrequest.CookieContainer = new CookieContainer();
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("PHPSESSID","86bte1ipainiq760vm2flv4h13"));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_u", "648113"));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_k", ""));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_sid", "3eebb0771a68c4a58581e495c34b2c93"));
using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
using (Stream resSteam = newresponse.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
richTextBox1.Text = sr.ReadToEnd();
This returns the game page as I want it to but can't figure out how to get the login working so that it will return the right cookies. On debugging the code the cookies that come back are completely different to the ones I'm seeing in fiddler so looks like it's just not logging in.
The code that you've written does a GET of the URL given; however, to retrieve the content of the page when you log in you need to pretend that your WebRequest is actually filling in the form, by passing in all the form variables and issuing a POST request instead.
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx gives a good walkthrough of the steps you require. The following isn't 100% complete, but should give you a step in the right direction:
var request = WebRequest.Create("http://www.conquerclub.com/login.php");
request.Method = "POST";
var parameters = new Dictionary<string, string> {
{ "username", "testuser1" },
{ "password", "testing" },
{ "redirect", "game.php?game=13025037" },
{ "direct", "63###www.conquerclub.com###" },
{ "protocol", "HTTP" },
{ "submit", "Login" } };
var content = string.Join( "&", ( from p in parameters select p.Key + "=" + HttpUtility.UrlEncode( p.Value) ).ToArray() ); ;
byte[] bytes = new byte[content.Length * sizeof(char)];
System.Buffer.BlockCopy(content.ToCharArray(), 0, bytes, 0, bytes.Length);
request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream dataStream = request.GetRequestStream();
dataStream.Write( bytes, 0, bytes.Length );
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();
Note that content includes various values that aren't visible on-screen; these can be seen by viewing the form's source, or (easier) by using the 'Network' tab on your browser debug toolbar of choice and monitoring the data being sent when the form is submitted.
You need to mimic the process of logging in. Look at the HTML code for the login page, and find out three things:
The <form> that sends the request, specifically the target (found in the attribute action) of the form.
Find out the method of sending the data (found in the attribute method)
Find the ID of the fields that represent the user name and password.
Having those you can easily construct a WebRequest that mimics a login.
Note, that this assumes a straight forward login screen (no AJAX, no CAPTCHAs).
I am using the following code on Page_Load of facebook_login page in asp.net web application. After redirecting this url I have got the accesstoken in url but when I try to access this current url via HttpContext.Current.Request.Url.AbsoluteUri it gave the url of app where it is published not the url which is currently in window.
How can I access this access token or userdetails?
var uriParams = new Dictionary<string, string>() {
{"client_id", facebookAppId},
{"response_type", "token"},
{"scope", "user_about_me, read_stream, email"},
{"redirect_uri", "http://apps.facebook.com/appNameHere/"}
};
StringBuilder urlBuilder = new StringBuilder();
foreach (var current in uriParams)
{
if (urlBuilder.Length > 0)
{
urlBuilder.Append("&");
}
var encoded = HttpUtility.UrlEncode(current.Value);
urlBuilder.AppendFormat("{0}={1}", current.Key, encoded);
}
var loginUrl = "http://www.facebook.com/dialog/oauth?" + urlBuilder.ToString();
Response.Redirect(loginUrl);
In Facebook you can set call back url value which should point to your web application. So once you make request to http://www.facebook.com/dialog/oauth and after successfully login Facebook redirect to Callback Url (which is your web application).
Then you have to make call to url : https://graph.facebook.com/oauth/access_token something like this:
StringBuilder uri = new StringBuilder();
uri.Append("https://graph.facebook.com/oauth/access_token?");
uri.Append("client_id=" + ClientKey + "&");
uri.Append("redirect_uri=" + Curl + "&");
uri.Append("scope=offline_access&");
uri.Append("client_secret=" + ClientSecret + "&");
uri.Append("code=" + OAuthCode);
HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization", String.Empty);
req.Method = "POST";
req.ServicePoint.Expect100Continue = false;
req.ContentLength = 0;
req.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string result = sr.ReadToEnd().Trim();
string[] resultCollection = Regex.Split(result, "&");
string access_token = Regex.Split(resultCollection[0], "=")[1];//This is the access token
//which you want and you can save it to database or some where for further use.
}
catch (WebException ex)
{
resp = (HttpWebResponse)ex.Response;
//pass on the exception
throw ex;
}
UPDATE :
OAuthCode you can get like this, it is available when after successfully login FB redirect to your web app page
OAuthCode = Request.Params["code"];
Header you can pass empty like String.Empty
I've just started playing around with C# few weeks ago. i got a task i am trying to perform and not sure if the way i do it now is the right approach.
I am trying to login to a website(in my case WordPress website- for lake of better options) and navigating in admin panel using C#
So far what I've done was creating a new project - Windows Form Application.
The following code - send a request to the website with password/username and other parameters as POST
private void button2_Click(object sender, EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
CookieContainer cookieJar2 = new CookieContainer(); // will use this later
string testaa = ""; // will use this later
string paramaters = "log=xxxx&pwd=xxxx&testcookie=1&redirect_to=http://www.example.com/wp-admin/&wp-submit=Log In";
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("http://www.lyndatobin-howes.com/wp-login.php");
requestLogin.Method = "POST";
requestLogin.AllowAutoRedirect = false;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
}
I then have this code to to take the cookie of the response.
HttpWebResponse response = (HttpWebResponse)requestLogin.GetResponse();
foreach (Cookie c in response.Cookies)
{
cookieJar2.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
then i have this to read the response + close some streams.
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
string responseFromServer = stIn.ReadToEnd();
webBrowser1.DocumentText = responseFromServer;
stIn.Close();
response.Close();
And then i try using the above cookie for the page i am trying to access as follows :
HttpWebRequest requestLogin2 = (HttpWebRequest)WebRequest.Create("http://www.example.com/wp-admin/");
requestLogin2.Method = "POST";
requestLogin2.AllowAutoRedirect = false;
requestLogin2.ContentType = "application/x-www-form-urlencoded";
requestLogin2.CookieContainer = cookieJar2;
requestLogin2.ContentLength = paramaters.Length;
StreamWriter stOut2 = new StreamWriter(requestLogin2.GetRequestStream(), System.Text.Encoding.ASCII);
stOut2.Write(paramaters);
stOut2.Close();
StreamReader stIn2 = new StreamReader(requestLogin2.GetResponse().GetResponseStream());
strResponse = stIn2.ReadToEnd();
string responseFromServer2 = stIn2.ReadToEnd();
webBrowser1.DocumentText = responseFromServer2;
richTextBox2.Text += "\n\n\n" + responseFromServer2;
stIn.Close();
Well it doesn't work for some reason I've been trying this for a week now.
I tried displaying the header - after the first request to see what headers i get back. and then looked at the cookie i built (cookieJar2) and it seem they aren't the same..
Anyways any help on the matter would be awesome and highly appreciated. i tried to give as much details as i could.
The first thing I notice about your code is that you call GetResponse() twice in your initial login:
HttpWebResponse response = (HttpWebResponse)requestLogin.GetResponse();
and
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
As a result, you're making the login request twice and will be getting back two different cookies. This will be why the cookie in cookieJar2 doesn't match what you're outputting later. Reuse your response object:
StreamReader stIn = new StreamReader(response.GetResponseStream());
Next, when you try to load the admin page, don't POST to it. Your second request should be a GET to retrieve the content of the page.
I have a winforms application i have been working on that runs multiple tests on consumer accounts. The tests require a one time login in order to execute.
string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
requestLogin.Method = "POST";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
This script works for the login just fine, the problem is when i need to actually run the tests i need to return all the results into a string (HTML results).
private string runTestRequest(Uri url, string parameters)
{
string testResults = string.Empty;
HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
runTest.CookieContainer = cookieJar;
runTest.Method = "POST";
runTest.ContentType = "application/x-www-form-urlencoded";
StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(parameters);
stOut.Close();
StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
testResults = stIn.ReadToEnd();
stIn.Close();
return testResults;
}
But it goes and tries to login. How can i use the Cookie from the previous login request with this + many other web requests?
Thanks for the help.
EDIT:
I added this to my code yet which should do the same thing as BrokenGlass is saying except just a little different but still doesn't work.
foreach (Cookie cookie in responseLogin.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
Something like this should work, I am using similar code to save a login cookie:
HttpWebRequest runTest;
//...do login request
//get cookies from response
CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
HttpCookie http_cookie = Request.Cookies[i];
Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}
//later:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
request.CookieContainer = myContainer;