1

So I manage to create a post requests but I have no idea on how to know if it worked? How can I get the html code of the website after the requests?

here is my code:

    var client = new WebClient();
    var method = "POST"; // If your endpoint expects a GET then do it.
    var parameters = new NameValueCollection();

    parameters.Add("utf8", "✓");
    parameters.Add("style", data);
    parameters.Add("size", size);
    parameters.Add("commit", "add to basket");

    var response_data = client.UploadValues(url_add_to_cart, method, parameters);

Moreover, after this post requests I need to do another post requests with the same session I used before. Is it possible by doing everything in the same webclient??

Thank you very much for your answers! If you need more info I'll add it

lucky simon
  • 241
  • 1
  • 6
  • 22

2 Answers2

1

Simply add a try catch block in this line :

try 
{
   var response_data = client.UploadValues(url_add_to_cart, method, parameters);
}
catch(Exception e)
{
   Console.WriteLine(e);
}

If you get an error on your HTTP request, program should execute the catch block. Try debbuging your code and see what is inside the response_data var.

HTTP error are metioned here btw :

https://www.npmjs.com/package/http-errors

Marech
  • 157
  • 12
  • It doesn't go in the catch! thank you! the problem is that I can't do another requests after that. it's like this one didn't do it. in python I used to do requests.session but I didn't find any equivalent in c# – lucky simon May 28 '18 at 17:15
  • Check this post to see what is inside your html page : https://stackoverflow.com/questions/3934508/printing-a-formatted-html-page-in-c-sharp and this post for request.session equivalent https://stackoverflow.com/questions/47490511/equivalent-of-pythons-request-session-auth-in-c-sharp-net – Marech May 28 '18 at 17:18
0

WebClient will throw WebException if the server returns a non-success (200) code.

Examine WebException.Status for more details.

https://msdn.microsoft.com/en-us/library/system.net.webexception.status.aspx

Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
  • it is kinda hard to understand ... I tried to print the html page of my page after and I saw that my post work. (so my requests adds an item to the bag), when I try to go on another page well my item is no longer in the page. I don't know how to fix that. I used requests.session in python but with c# I find nothing on this – lucky simon May 28 '18 at 17:04