I am currently working on a class that helps me creating WebRequest with cookies and POST parameters. But the response.cookies is always empty. How can i grab the returned cookies and send them with the next request?
class WebHandler
{
private string lasturl = "";
private Dictionary<string, Cookie> cookies;
public string lastContent;
public WebHandler()
{
cookies = new Dictionary<string, Cookie>();
}
public HttpWebResponse request(string address)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "GET";
if(lasturl != "")
request.Referer = lasturl;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}
public HttpWebResponse request(string address, Dictionary<string, string> postParameters)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "POST";
string postData = "";
foreach (string key in postParameters.Keys)
postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postParameters[key]) + "&";
byte[] data = Encoding.ASCII.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}
}
Since this.cookies is initially null, your code never enters the loop
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
Bad part of it that you never see request.CookieContainer is null (Otherwise you would get null reference exception) which would show you something is wrong.
Since request.CookieContainer is null, HttpWebResponse never gets the returned cookies.
Solution: Easy, Just add
if (request.CookieContainer == null)
request.CookieContainer = new CookieContainer();
after
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
PS: You may want to read this question about Naming Conventions in c#.
PS2: in a method request using a variable name request doesn't improve readability.
I had the same problem, so I added request.AllowAutoRedirect=false;, now I can get cookies:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
...
request.Method = "POST";
request.AllowAutoRedirect=false;
If the HttpWebResponse object's Cookies collection is empty, then that means the response had no new cookies. To get access to all cookies for request and response, then read the cookies from the HttpWebRequest object instead, like this:
foreach (Cookie newCookie in request.Cookies)
{
// Do something with cookies here for next request
}
Related
How can i iterate a GET response if i dont know the structure i will get in advance.
Lets say i can get this response:
{
"hostName": "host",
"domainName": "domain",
}
and the next time i send the same GET request i get this response:
{
"hostName": "host",
}
my code so far:
public static void GetRequest(string url)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
String userId = "userID";
String userPassword = "userPassword";
NetworkCredential networkCredential = new NetworkCredential(userId, userPassword);
httpWebRequest.Credentials = networkCredential;
httpWebRequest.MaximumAutomaticRedirections = 3;
httpWebRequest.Timeout = 5000;
System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(userId + ":" + userPassword));
httpWebRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
Console.WriteLine("Sending HTTP Request");
var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var responseStream = httpWebResponse.GetResponseStream();
if (responseStream != null)
{
var streamReader = new StreamReader(responseStream);
Console.WriteLine("HTTP Response is: ");
Console.WriteLine(streamReader.ReadToEnd());
}
if (responseStream != null) responseStream.Close();
}
So I'm making a small application for a vbulleting site but need to authenticate the user when he opens my application, I have code to send login request, but I am unsure how to actually check if the login was successful.
This is what I have so far:
public string Login(string username, string password)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MPGHLogin);
string cookie = "";
string values = "vb_login_username=" + username + "&vb_login_password=" + password
+ "&securitytoken=guest&"
+ "cookieuser=checked&"
+ "do=login";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = values.Length;
CookieContainer a = new CookieContainer();
req.CookieContainer = a;
System.Net.ServicePointManager.Expect100Continue = false;
using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
writer.Write(values);
}
HttpWebResponse c = (HttpWebResponse)req.GetResponse();
foreach (Cookie cook in c.Cookies)
{
cookie = cookie + cook.ToString() + ";";
}
if (c.StatusCode != HttpStatusCode.OK)
return "FAILED_CONNECTION";
return cookie;
}
But how can I check if the authentication was successful?
For other people that may have the same problem, I completely forgot about checking the respone stream for a login successful message, so below is the full code.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MPGHLogin);
req.AllowAutoRedirect = true;
string cookie = "";
string values = "vb_login_username=" + username + "&vb_login_password=" + password
+ "&securitytoken=guest&"
+ "cookieuser=checked&"
+ "do=login";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = values.Length;
CookieContainer a = new CookieContainer();
req.CookieContainer = a;
System.Net.ServicePointManager.Expect100Continue = false;
StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
writer.Write(values);
writer.Close();
req.Timeout = 5000;
HttpWebResponse c;
try {
c = (HttpWebResponse)req.GetResponse(); // da response
} catch(Exception e)
{
MessageBox.Show(e.Message, "Web Exception");
return "WebException";
}
foreach (Cookie cook in c.Cookies)
{
cookie = cookie + cook.ToString() + ";";
}
Stream resp = c.GetResponseStream();
StreamReader reader = new StreamReader(resp, Encoding.GetEncoding(c.CharacterSet));
string response = reader.ReadToEnd();
reader.Close();
reader.Dispose();
if (response.Contains("Thank you for logging in, " + username))
{
c.Dispose();
return cookie;
}
else
return "FAILED_AUTH";
my question is, how can I call multiple requests via HttpWebRequest with same authenticate cookie in C#? I tried a lot of times but for now I dunno how to do it :/
My code is below:
var postData = "method=loginFormAccount&args[0][email]=###&args[0][pass]=###&args[0][cache]=37317&args[]=1";
var data = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("###");
request.CookieContainer = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var cookies = new CookieContainer();
cookies.Add(response.Cookies);
System.IO.File.WriteAllText(#desktop + "\\post.html", new StreamReader(response.GetResponseStream()).ReadToEnd());
// =================================== END LOGIN ==================================== \\
System.IO.File.WriteAllText(#desktop + "\\cookie.html","");
foreach (Cookie cook in response.Cookies)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#desktop + "\\cookie.html", true))
{
file.WriteLine(cook.ToString());
}
// Show the string representation of the cookie.
}
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer = cookies;
requestNext.Method = "GET";
HttpWebResponse responseNext = (HttpWebResponse)requestNext.GetResponse();
//var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
System.IO.File.WriteAllText(#desktop + "\\get.html", new StreamReader(responseNext.GetResponseStream()).ReadToEnd());
My main problem is that, cookie which I'm getting is the cookie BEFORE authenticate so I must to do something to get cookie AFTER authenticate.
Try this :
HttpWebRequest requestNext = (HttpWebRequest)WebRequest.Create("####");
requestNext.CookieContainer.Add(cookies);
I have a requirement to get cookies from login because i will apply this cookie in the same page. Here is my code
string boundary = "41184676334";
string username = "username";
string password = "password";
string login_post_data =
"POSTDATA=-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"login\"" +
"\n\n" + username +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"key\"" +
"\n\n" + password +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"clcode\"" +
"\n\n" +
"\n-----------------------------" + boundary + "--";
var cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://site.com/cgi-bin/login.pl?logout");
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=---------------------------" + boundary;
request.Host = "site.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Referer = "https://site.com/cgi-bin/login.pl?logout";
//request.Headers.Add("POSTDATA", post_data);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(StrToByteArray(login_post_data), 0, StrToByteArray(login_post_data).Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string temp = reader.ReadToEnd();
richTextBox1.Text = temp;
}
foreach (string c in response.Cookies)
{
listBox1.Items.Add(c);
}
The thing is "https://site.com/cgi-bin/login.pl?logout" is the same page after i logged in and i need to pass the cookie
It looks like you try to log in and log out at the same time with your request, sent to URL
https://site.com/cgi-bin/login.pl?logout
Remove the ?logout parameter and try again. Please update your question if it doesn't change anything.
Please explain further, what you're trying to achieve, so we can discuss if the code is correct.
I have a foreach with an "If" and when the condition is true, I do a WebResponse to post my item in a server.
Sometimes the code run for two o more items but other times crashes with the following error:
The remote server returned an error: (407) Proxy Authentication Required.
The code:
WebClient client = new WebClient();
string authInfo = "admin:geoserver";
string address = "http://xxxxxxxx:8080/geoserver/rest/workspaces/";
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
WebRequest request = WebRequest.Create(address);
request.ContentType = "text/xml";
request.Method = "POST";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
byte[] bret = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>" + nameWS + "</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(bret, 0, bret.Length);
reqstr.Close();
try
{
WebResponse response = request.GetResponse();
response.Close();
}
My Environment is C# Visual Studio 2010
how often do you call this? as others have suggested it could be that the server is protected from DOS and your requests are seen like that. It's also valuable to dispose immediately all disposable objects with a using block for example. we had some issues once while leaving too many connections open to our web server, internally in our network. You could adjust your code to look like this:
using(var client = new WebClient())
{
string authInfo = "admin:geoserver";
string address = "http://xxxxxxxx:8080/geoserver/rest/workspaces/";
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
var request = WebRequest.Create(address);
request.ContentType = "text/xml";
request.Method = "POST";
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
byte[] bret = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>" + nameWS + "</name></workspace>");
using (var reqstr = request.GetRequestStream())
{
reqstr.Write(bret, 0, bret.Length);
}
try
{
using (var response = request.GetResponse())
{
// your code here...
}
}
catch (Exception exc)
{
System.Diagnostics.Debug.WriteLine(exc.Message);
}
}