multiple post requests same session c# - c#

I'm coding a script that goes on a website, adds to cart an item, and checkout.
I manage to add to cart but when I want to checkout it's like nothing is in the cart.
How can I add to cart/ checkout using the same session?
Here is my code:
var request = (HttpWebRequest)WebRequest.Create(url_add_to_cart);
var postData = "utf8=✓";
postData += "style=" + data_style_id;
postData += "size=" + size;
postData += "commit=add to basket";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
//checkout----------------
var url_checkout = link_general + "/checkout.json";
var request2 = (HttpWebRequest)WebRequest.Create(url_checkout);
var postData2 = "utf8=✓";
postData2 += "order[billing_name]=toto";
postData2 += "order[email]=toto#gmail.com";
var data2 = Encoding.ASCII.GetBytes(postData2);
request2.Method = "POST";
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = data2.Length;
using (var stream2 = request2.GetRequestStream())
{
stream2.Write(data2, 0, data2.Length);
}
var response2 = (HttpWebResponse)request2.GetResponse();
var responseString2 = new StreamReader(response2.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString2);
When I do the checkout request it doesn't work and get the source corde of the website html home page
Thank you very much for your answers

You need to store request.CookieContainer in local variable and every time you need to send new request set it again
private CookieContainer cookieContainer;
private void SendRequest()
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
if (this.cookieContainer != null)
request.CookieContainer = this.cookieContainer;
else
request.CookieContainer = new CookieContainer();
...
...
...
this.cookieContainer = request.CookieContainer;
}
And add & to end of postData lines

Related

Why is my HttpWebRequest unable to post data, did I get the code wrong?

string url = "http://localhost/panas/index.php/api/v1/data";
var request = (HttpWebRequest)WebRequest.Create(url);
var postData = "nama=" + txtEmail.Text;
postData += "&status=" + txtFName.Text;
postData += "&keterangan=" + txtLName.Text;
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();

How can I Post data using HttpWebRequest?

I have this HttpWebRequest:
var request = HttpWebRequest.Create("http://example.com/api/Phrase/GetJDTO");
request.ContentType = "application/json";
request.Method = "POST";
But I need to add a payload to the body of the request like this:
Jlpt = 2
Can someone help and tell me how I can add data to the POST ?
You can do by this
var request = HttpWebRequest.Create("http://example.com/api/Phrase/GetJDTO");
var postData = "Jlpt = 2";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
but I suggest you use HttpClient rather than HttpWebRequest in this case
if (data != null)
{
request.ContentType = "application/json";
using (var stream = new StreamWriter(request.GetRequestStream()))
{
var serialized = JsonConvert.SerializeObject(data);
stream.Write(serialized);
}
}
else
{
request.ContentLength = 0;
}
where data is any object you want to send

Web Request Not Returning Anything

I'm trying to login to this website here: https://freebitco.in/
So I set up a web request like so:
var request = (HttpWebRequest)WebRequest.Create("https://freebitco.in");
var postdata = "op=login&btc_address=BTCADDRESS&password=PASSWORD";
var data = Encoding.ASCII.GetBytes(postdata);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length; //1PkhThc9hCXpdvcThtwX3SzbfmTzDFxL1h:bigken <= BTC Login
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
MessageBox.Show(responseString);
The messagebox returns to me nothing though, blank. I'm not sure why this is happening so I was hoping someone could shed some light on this? I've also tried using a Web Client, and it gave me the same result. Thank you guys.
EDIT: Here's the Fiddler Raw data: http://pastebin.com/WUEvq6D5
Edit 2: Tried encoding the POST data and now it returns the login page
Updated Code:
var request = (HttpWebRequest)WebRequest.Create("https://freebitco.in");
var postdata = "op=login&btc_address=ADDRESS&password=PASS";
var encoded_data = HttpUtility.UrlEncode(postdata);
var data = Encoding.ASCII.GetBytes(encoded_data);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

how to use CookieContainer

Cookiecontainer is not set some thing after send request via HttpWebRequest.
i use live httpheader chorme, and result after login
Set-Cookie: SHARINGSESSID4S=9jpuh21oi82s10mn6cvomjq190; expires=Mon, 29-Sep-2014 16:09:22 GMT;path=/; domain=.4share.v
however, i only get SHARINGSESSID4S=9jpuh21oi82s10mn6cvomjq190.
I cant use cookie for sub request.
my code:
private void button1_Click(object sender, EventArgs e)
{
CookieContainer cookie = new CookieContainer();
Uri site=new Uri("http://4share.vn/index/login");
var request = (HttpWebRequest)WebRequest.Create(site);
var postdata = "username=XXXXX&password=XXXXX";
var data = Encoding.ASCII.GetBytes(postdata);
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.CookieContainer = cookie;
var stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
MessageBox.Show(cookie.GetCookieHeader(site));
var response = (HttpWebResponse)request.GetResponse();
MessageBox.Show(cookie.GetCookieHeader(site));
Uri site2 = new Uri("http://4share.vn/f/487b7d717a717b71/V-Z%20Naruto.Shippuden.Ultimate.Ninja.Storm.Revolution-CODEX#Jenty.VNZ.iso");
var request2 = (HttpWebRequest)WebRequest.Create(site2);
request2.CookieContainer = cookie;
var response2 = (HttpWebResponse)request2.GetResponse();
MessageBox.Show(cookie.GetCookieHeader(site2));
string Text = new StreamReader(response2.GetResponseStream()).ReadToEnd();
if (Text.IndexOf(" FileDownload: ") > 0) MessageBox.Show("success");
}

disqus API missing required parameter: grant_type

I am trying to retrieve an access_token through the disqus API.
This is my url:
https://disqus.com/api/oauth/2.0/access_token/?client_id=cvwNO7HaRwgYDq9anat8j7uzowJ8HBEz8gH7mUnmMhC0BKZZTkObc5d7o242liNG&grant_type=authorization_code&client_secret=Hrrgy1ZLcLN0qjmZhzXR2owET8cGazcbcGNxTlsWEJYiNfc3JcQLbKx2PYW6yNU7&redirect_uri=http://www.aftenposten.no&code=PM6QYwUJ
i'm getting an error: missing required parameter: grant_type
i'm using the following code to get the response:
HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(getAccessTokenResponse), request);
You need to pass the form data in the request body and not as url parameters. Give this a try:
var uri = "https://disqus.com/api/oauth/2.0/access_token/"
HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
var data = "client_id=[your client id]"
data += "&grant_type=authorization_code"
data += "&client_secret=[your client secret]"
data += "&redirect_uri=http://www.aftenposten.no&code=PM6QYwUJ"
var postData = Encoding.ASCII.GetBytes(data);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Categories