How to simulate browser HTTP POST request and capture result in C#

前端 未结 1 1375
南笙
南笙 2020-12-01 08:34

Lets say we have a web page with a search input form, which submits data to server via HTTP GET. So that\'s mean server receive search data through query strings. User can s

相关标签:
1条回答
  • 2020-12-01 08:45

    You could take a look at the WebClient class. It allows you to post data to an arbitrary url:

    using (var client = new WebClient())
    {
        var dataToPost = Encoding.Default.GetBytes("param1=value1&param2=value2");
        var result = client.UploadData("http://example.com", "POST", dataToPost);
        // do something with the result
    }
    

    Will generate the following request:

    POST / HTTP/1.1
    Host: example.com
    Content-Length: 27
    Expect: 100-continue
    Connection: Keep-Alive
    
    param1=value1&param2=value2
    
    0 讨论(0)
提交回复
热议问题