After logged in, cookies are generated for subsequent requests.
I am trying to get cookies by sending a "POST" request.
In the response header, there is a "set-cookies" field and its value is like: "value1=value1,value2=value2......";
However, the CookieContainer i created for the HttpWebRequest is still empty.
So what is happening and how can I have my CookieContainer filled?
Any suggestion would be appreciated.
string loginData = "################################";
CookieContainer cookiecontainer = new CookieContainer();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.ftwilliam.com/cgi-bin/Enter.cgi");
myRequest.CookieContainer = cookiecontainer;
myRequest.Method = "POST";
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = false;
myRequest.Referer = "https://www.ftwilliam.com/cgi-bin/Enter.cgi";
myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
myRequest.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
myRequest.ContentLength = loginDataBytes.Length;
Stream stream = myRequest.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
Related
I want login to website and get html code after login successful.
https://viralstyle.com/login
Error: remote server returned an error 500 line: HttpWebResponse
webResp = (HttpWebResponse)req.GetResponse();
string email = "email#email.com";
string pw = "mypassword";
string PostData = String.Format("email={0}&password={1}&remember=0", email, pw);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://viralstyle.com/login");
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentLength = PostData.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(PostData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();
Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
webBrowser1.DocumentText = reader.ReadToEnd();
I'm trying to log in to website. Following settings works when i use putty:
GET /WindchillCProdA/netmarkets/jsp/netmarkets/view.jsp HTTP/1.1
Host: www.host_address.com
Cookie: action_number=0
Authorization: Basic bG9naW46cGFzc3dvcmQ=
I've used following code to make the same thing in C#:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.webpage.com/WindchillCProdA/netmarkets/jsp/netmarkets/view.jsp");
request.Method = "GET";
CookieContainer gaCookies = new CookieContainer();
gaCookies.Add(new Cookie("action_number", "0") { Domain = request.Host });
request.Host = www.host_address.com;
request.CookieContainer = gaCookies;
request.UserAgent = "HTTP/1.1";
request.Headers["Authorization"] = "Basic " + "bG9naW46cGFzc3dvcmQ=";
I'm receiving:
The remote server returned an error: (401) Unauthorized.
I suspect that i have to fill somehow view.jsp because my parameters are not used in this case.
use post method and write on request stream the values,
i Checked the web site and i was redirected to another page the code should be as bellow
//there could be some hash check the scripts via inspect Element
string postData = String.Format("email={0}&newPassword={1}", "yourusername","yourPassword");
var request = (HttpWebRequest)WebRequest.Create("https://secure.systemsecure.com/host/login/23.htm");
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.CookieContainer = new CookieContainer();
getRequest.UserAgent ="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko)Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version10;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
var getResponse = (HttpWebResponse)getRequest.GetResponse();
im trying to send POST request using HttpWebRequest and fiddler shows me that im sending GET?
any help will be appreciated since im able to see what im doing wrong.
code :
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LOGIN_API_BASE_URL);
string postString = string.Format("api_email={0}&api_password={1}", EMAIL, PASSWORD);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postString);
CookieContainer cookies = new CookieContainer();
request.CookieContainer = cookies;
//request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Timeout = i_timeout;
//request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//request.Referer = "https://accounts.craigslist.org";
using (Stream writer = request.GetRequestStream())
{
writer.Write(data, 0, data.Length);
//writer.Close();
}
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
request.GetResponse().Close();
ok found the problem, the url im sending the request to is http://domain.com and when i send it to http://www.domain.com it works and send a post. so new question why is that? ( the answer will get the answer to my question as i dont like the idea of voting my self )
I'm trying to login on facebook and retrive a token by using this link:
https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token
My code looks like this, but i get an invalid link when i'm requesting the link above.
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/login.php?login_attempt=1");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string postData = String.Format("email={0}&pass={1}", email, pass);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies); //recover cookies First request
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
cookies = getResponse.Cookies;
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//Get the token
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token");
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
webRequest.AllowAutoRedirect = false;
HttpWebResponse rresponse = (HttpWebResponse)webRequest.GetResponse();
if (rresponse.StatusCode == HttpStatusCode.Redirect)
{
Console.WriteLine("redirected to: " + rresponse.GetResponseHeader("Location"));
}
Please help me. Thanks in advance.
https://www.facebook.com/dialog/oauth?client_id=282892925078054&redirect_uri=https://www.facebook.com/&response_type=token
The URL does not allow the application configuration.: The application settings do not allow one or more of the URLs. Internet Site URL and Canvas URL or domain URLs must be sub-domains of application domains.
I want to login to a website using a webclient or httpwebrequest, after i'm logged in i want to navigate to a page in the site.
so i know how to post data and to login, my problem is with the navigation after the login,
it redirect me to the login page, i know it has to do with the cookies, but i dont understand what i'm doing worng... here is my code -> it is taken from another answear on stackoverfllow... (http://stackoverflow.com/questions/8425593/c-sharp-httpwebrequest-post-login-to-facebook)
string getUrl = "somesite.com";
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl2 = "somepage.login.com";
string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//untill here it works fine i'm logged in....
HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
getRequest2.CookieContainer = new CookieContainer();
getRequest2.CookieContainer.Add(cookies);
HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();
cookies = getResponse2.Cookies;
using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//here it rediects me to login page
any suggestions> what i'm doing wrong
Do not instantiate CookieContainer for each request. You need one CookieContainer over all requests. Try this:
CookieContainer cc = new CookieContainer();
string getUrl = "somesite.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string getUrl2 = "somepage.login.com";
string postData = String.Format("username={0}&userpassword={1}", "foo", "foofoo");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl2);
getRequest.CookieContainer = cc;
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//untill here it works fine i'm logged in....
HttpWebRequest getRequest2 = (HttpWebRequest)WebRequest.Create("a page on the site..");
getRequest2.CookieContainer = cc;
HttpWebResponse getResponse2 = (HttpWebResponse)getRequest2.GetResponse();
using (StreamReader sr = new StreamReader(getResponse2.GetResponseStream()))
{
string sourceCode = sr.ReadToEnd();
}
//here it rediects me to login page