C# 通过 ASP.net 发送 HTTP Post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10797995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:17:56  来源:igfitidea点击:

Send HTTP Post Request through ASP.net

c#asp.netformshttp-post

提问by asad

I've hit a road block due to this problem in my project, Any kind of help will be highly appreciated.

由于我的项目中的这个问题,我遇到了障碍,任何形式的帮助都将受到高度赞赏。

My problem is that I want users to enter (their) destination & email at my website. Then I'll take those values and fill in the text fields at the following website and then click the "request measurement button". In short a simple HTTP Post request.

我的问题是我希望用户在我的网站上输入(他们的)目的地和电子邮件。然后我将获取这些值并填写以下网站上的文本字段,然后单击“请求测量按钮”。简而言之,一个简单的 HTTP Post 请求。

http://revtr.cs.washington.edu/

http://revtr.cs.washington.edu/

My C# code is as follows:

我的 C# 代码如下:

    // variables to store parameter values
    string url = "http://revtr.cs.washington.edu/";

    // creates the post data for the POST request
    string postData = ("destination=www.thechive.com&node=161&email=abc%40xyz.edu.pk&prediction=If+you+believe+you+know+the+traceroute+%28for+instance%2C+if+you+are+trying+out+our+system+using+a+destination+that+you+actually+control%29%2C+please+cut-and-paste+the+traceroute+into+this+box+to+aid+us+in+improving+our+system.");

    // create the POST request
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = postData.Length;

    // POST the data
    using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
    {
        requestWriter2.Write(postData);
    }

    //  This actually does the request and gets the response back
    HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

    string responseData = string.Empty;

    using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
    {
        // dumps the HTML from the response into a string variable
        responseData = responseReader.ReadToEnd();
    }

    //  Now, find the index of some word on the page that would be 
    //     displayed if the login was successful
    int index = responseData.IndexOf("Measuring");

    if (index > -1)
        ListBox1.Items.Add("SUCCESS");

But the responseDatashows that the BUTTON hasn't been clicked when the program is run (I got this info from debugger of VS2010)

但是responseData显示程序运行时没有点击BUTTON(我从VS2010的调试器中得到了这个信息)

采纳答案by jjchiw

it seems the url must be

看来网址必须是

string url = "http://revtr.cs.washington.edu/measure.php";

since is the action of the form.

因为是表单的动作。