0

I am logging into a website using WebClient.UploadData (), but to be able to perform any type of operation after logging in to the site, I can not do it. I have already tried with httpclient and can not.

var wc = WebClient();
string post = HttpUtility.UrlEncode("ctl00$ContentMain$login") + "=" + login +"&" + HttpUtility.UrlEncode("ctl00$ContentMain$password") + "=" + password + "&" + HttpUtility.UrlEncode("ctl00$ContentMain$btnCompletesummarized") + "=" + HttpUtility.UrlEncode("Complete summarized");

byte[] resposta = wc.UploadData(url, "POST", UTF8Encoding.Default.GetBytes(post));
resultado = UTF8Encoding.UTF8.GetString(resposta);

with this code I can log in to the site, but to be able to perform an operation like clicking on the logged in page I can not.

button after login:

<input type = "submit" name = "btnCompleteConsultation" value = "Complete Consultation" id = "btnCompleteConsultation">

Example: I can log into my facebook with WebClient.UploadData (), but after login to be able to click on the button like "messenger" I can not simulate with WebClient.

Would you guys have any tips that can help me with this problem?

Carillo_
  • 21
  • 2
  • 1
    What exactly is it that *works* and what does *not work*? You need to be more specific – mortb Apr 02 '19 at 14:36
  • Hi mortb Example: I can log into my facebook with WebClient.UploadData (), but after login to be able to click on the button like "messenger" I can not simulate with WebClient. Would you have any tips that can help me in this problem? – Carillo_ Apr 02 '19 at 15:05
  • 1
    You would need to use something like a WebBrowser control. What you're attempting is possible but difficult because the website is designed for human user interaction. If you script it, they could change the site so that what works now doesn't work later, even though a human user wouldn't see the difference. They might even detect a number of rapid requests and block you. It's better to see if they have a public API you can work with which is intended to allow you to write code to interact with them. – Scott Hannen Apr 02 '19 at 15:09
  • Check out [this documentation](https://developers.facebook.com/docs/apis-and-sdks/). To the extent that they will let you write code to work with their site, they provide tools and documentation to make it easier. – Scott Hannen Apr 02 '19 at 15:11
  • Obrigado pela dica mortb e Scott Hannen , mas eles não tem uma API por enquanto para que eu possa consumir. https://tenor.com/view/spiderman-tobey-maguire-cry-sad-emotional-gif-5056896 – Carillo_ Apr 02 '19 at 16:01
  • I think selenium web driver could be used for this http://scraping.pro/example-of-scraping-with-selenium-webdriver-in-csharp/ (selenium is many times used for automated gui testing, but I think it could be applied to your use case) it allows you to find elements on the page and send events to the elements – mortb Apr 02 '19 at 17:23
  • It is not really facebook that you are trying to use, that's just an example, right? – mortb Apr 02 '19 at 17:26

1 Answers1

0

What are you trying to achieve? I see that you're using a WebClient to make a POST to that URL. What do you expect to get? The response code (200, 500, 404), the full HTML? As a starting point, if you only want to know if the response from the server was an OK, you can do:

    WebRequest request = WebRequest.Create("http://www.google.com");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

The response will have a StatusCode property and some other useful information about the request and it's response, like the charset and the cookies (that you'll probably need if you're trying to maintain a session -i.e., remain logged in-).

If you want the full HTML response, you can get the stream from the response:

    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);

Notice that if you're expecting any client-side content that would come with the response (like Javascript), you'll have to use something a little more hacky, like a WebBrowser that simulates the navigation. For that case, here's some useful discussions: (Non-threaded solution): C# httpwebrequest and javascript (Threaded solution): WebBrowser Control in a new thread

Edit: From the added details on your question, it seems that you're trying to simulate a full Facebook navigation experience programmatically. Your interest is probably pointing to the results (the unread messages from a given Facebook account, for example). If that's the case, you might want to look for some API solution. Handling raw HTML to interact with web pages is rarely the best option. Good luck!

  • 1
    Hi, sorry for not explaining it right. I want to get the html page. Thank you Villa Mariano Luis – Carillo_ Apr 02 '19 at 15:10
  • NP, asking questions correctly is often quite hard. Anyways, for the HTML, I would get it as a stream (the second example), and read from there, like this: string responseString = reader.ReadToEnd(); – Mariano Luis Villa Apr 02 '19 at 15:17
  • In that case, if you did a post doing a search and if you wanted to get the html from the first page of the google prucura options, how could you do this with HttpWebResponse / WebRequest? – Carillo_ Apr 02 '19 at 15:18
  • I think you're taking a web scrapping approach for a problem that should be solved with an API. I once did something like that, by concatenating a search query into Google's URL (something like: google.com/search?blabla&q=MyQuery -this isn't how it actually looks like), inspecting the HTML in search for the URL to the first result, and doing a GET request to that URL. Once again, this is suboptimal, and I highly recomend you to search for an API solution that'll give you some JSON or XML response which is way easier to handle. – Mariano Luis Villa Apr 02 '19 at 15:32
  • If they had an API it would be a wonder, problem is that they have not. https://tenor.com/view/cute-sad-tears-gif-4544076 – Carillo_ Apr 02 '19 at 15:55