I've got a piece of code the goes out and does an HttpWebRequest to Google Wallet. The code works just fine on all my machines, except for this one computer at work that I'm on right now. The Internet on this computer works just fine (as I'm using it right now to type this question) and we have no proxy server at work.
The problem is that it just hangs. It doesn't even timeout. It just hangs with no error message or anything and I have to force close it. Here is the code:
private static string GetLoginHtml()
{
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
var cookieJar = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieJar;
using (var requestStream = request.GetRequestStream())
{
string content = "Email=" + Username + "&Passwd=" + Password;
requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
string galxValue = ParseOutValue(html, "GALX");
return GetLoginHtml2(galxValue, cookieJar);
}
}
}
Stepping through the code on the Visual Studio debugger, I know that it hangs when it hits the following line:
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
Specifically, the request.GetResponse() part is what's hanging. Running Fiddler, all I see is a gray entry that looks like this:
3 200 HTTP Tunnel to accounts.google.com:443 0
There is nothing else. No response body. Do anyone have any suggestions on what could be going on? The fact that its happening on just this one computer tells me that it may not be a programming issue.
I finally figured out the issue with this. I'm posting this answer in hopes it might help someone else with some inevitable head scratching in the future.
Here is my working code now:
private static string GetLoginHtml()
{
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
var cookieJar = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieJar;
using (var requestStream = request.GetRequestStream())
{
string content = "Email=" + Username + "&Passwd=" + Password;
requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);
}
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
string galxValue = ParseOutValue(html, "GALX");
return GetLoginHtml2(galxValue, cookieJar);
}
}
My guess is that my requestStream wasn't being garbage collected and closed before getting the response stream. I think the response stream was being open and read before the request stream finished writing its bytes. Silly .NET garbage collector
Related
So I am currently learning HttpWebRequests & Responses because I love the idea of automating my online work for me.
So after reading a bit about it I decided to start by something simple, maybe login to a website because that's something I do quite frequently.
I decided to try it with Spotify because the website is just so plain and simple and I thought that it would be a great place to start at.
So what I have so far is this but I keep getting an error
System.Net.WebException: 'The remote server returned an error: (405)
Method Not Allowed.'
Are there a specific set of steps I should follow when trying to login? What am I looking for? I used fiddler to grab ahold of the postData parameters (Not sure if those are the right ones, I think I need more)
If I am missing any steps feel free to fill me in on what I am doing wrong here.
protected static CookieContainer Login()
{
string userName = "myUsername";
string password = "myPassword";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=" + userName + "&password=" + password;
byte[] postDataBytes = encoding.GetBytes(postData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/en-EN/login");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = postDataBytes.Length;
httpWebRequest.AllowAutoRedirect = false;
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(postDataBytes, 0, postDataBytes.Length);
stream.Close();
}
var cookieContainer = new CookieContainer();
//This is where it throws the error
using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
foreach (Cookie cookie in httpWebResponse.Cookies)
{
cookieContainer.Add(cookie);
}
}
}
return cookieContainer;
}
I have the following code:
string url = string.Format("http://{0}:{1}/{2}/xml",Address.Host,Address.Port,Address.ContextRoot);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.UserAgent = "Foo";
req.ContentType = "text/xml; charset=\"UTF-8\"";
req.KeepAlive = false;
using (Stream reqStream = req.GetRequestStream())
{
SoapEnvelope s = new SoapEnvelope(Address, null);
XmlDocument xmlDoc = s.GenerateXml(message);
xmlDoc.Save(reqStream);
}
result = ReceiveAnswer(req);
private string ReceiveAnswer(HttpWebRequest req)
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
using (Stream resStream = res.GetResponseStream())
using (StreamReader reader = new StreamReader(resStream))
{
if (res.StatusCode != HttpStatusCode.OK)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Failed to post data to http://{0}:{1}", Address.Host, Address.Port).AppendLine();
sb.AppendFormat("Response: {0} {1}", res.StatusCode, res.StatusDescription);
res.Close();
throw new WebException(sb.ToString());
}
String resData = reader.ReadToEnd();
return resData;
}
}
This code works fine, when I'm connecting to an existing server, but I've just tested, that if the server I specify does not exist then this part: (HttpWebResponse)req.GetResponse() will hang. I expected to get a webexception or something in this scenario, because I want to catch it.
I've read it somewhere that this Response has a timeout of 100 seconds, I've waited more than that, but nothing happened. Then I set the Timeout property to 15 seconds, but that didn't help either.
Anyone knows what should I do?
There was no problem with my code, an other part of the company's code causes the problem.
Be very careful with what you're doing.
Timeout method does not apply to the time waiting for the response to be caught.
Timeout counts for the entire life of your connection (meaning request + response).
If you're then setting a too-low timeout time, your program will never catch it. Maybe you could try to raise the Timeout number?
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 used following code to get the access token from code as below
String code = HttpContext.Current.Request["code"];
string redirecturl = HttpContext.Current.Request["url"];
string Url = "https://accounts.google.com/o/oauth2/token";
string grant_type = "authorization_code";
string redirect_uri_encode = UrlEncodeForGoogle(url);
string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type={5}";
HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
string result = null;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
string param = string.Format(data, code,configurationInfo.oauthclientid , configurationInfo.oauthclientsecretid, redirect_uri_encode, grant_type, "offline");
var bs = Encoding.UTF8.GetBytes(param);
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse response = request.GetResponse())
{
var sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
}
i am getting response as
The remote server returned an error: (400) Bad Request.
i do not know where i went wrong
waiting for your valuable comments
Google also provides a higher level library for accessing its services. I find it makes it much easier to work with its APIs.
http://code.google.com/p/google-api-dotnet-client/
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.