简体   繁体   中英

HTTP Requests in C#

I need to visit a URL, find a specific text box in said page - fill it with data and then submit a form.

How can I accomplish this in C#?

PS Innocent intentions.

You'd be best looking at the WebRequest class (System.Net).

You'll want to look at the POST Method to post a form (click the submit button with the required fields completed).

Example:

    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.
    string postData = "This is a test that posts this string to a Web server.";
    byte[] byteArray = Encoding.UTF8.GetBytes (postData);

There is a nice tutorial and lots of information on MSDN here . (Continuation of above source code)

if you visit the page in a browser and do the stuff you need manually with fiddler2 installed and activated you can see which requests get sent to the webserver.

All you need to do is to replicate those requests (form posts) using for example the WebRequest class or the WebClient class in the .net framework.

WatiN is also an alternative.

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