如何模拟在C#中的浏览器HTTP POST请求,并捕捉结果 [英] How to simulate browser HTTP POST request and capture result in C#

查看:680
本文介绍了如何模拟在C#中的浏览器HTTP POST请求,并捕捉结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个搜索输入的形式,它通过HTTP GET将数据提交到服务器的网页。所以,这意味着服务器通过查询字符串接收的搜索数据。用户可以看到的网址,也可以通过初始化自己这个请求(通过URL +查询字符串)。

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 see the URL and can also initialize this request by himself (via URL + Query strings).

我们都知道这一点。这里是个问题。

We all know that. Here is the question.

如果此网页提交数据通过HTTP POST服务器?用户如何通过自己初始化这个请求?

What if this web page submits data to the server via HTTP POST? How can user initialize this request by himself?

嗯,我知道如何捕捉HTTP POST(这就是为什么网络嗅探器是),但我怎么能由我自己在C#code模拟这个HTTP POST请求?

Well I know how to capture HTTP POST (that's why network sniffers are for), but how can I simulate this HTTP POST request by myself in a C# code?

推荐答案

您可以看看在的 WebClient的类。它可以让你的数据发布到任意网址:

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

这篇关于如何模拟在C#中的浏览器HTTP POST请求,并捕捉结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆