简体   繁体   中英

httpclient to simulate the navigation of a user on Internet c#

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?

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 -ie, 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM