Send HTTP Post Request through ASP.net

空扰寡人 提交于 2019-12-24 10:45:55

问题


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://revtr.cs.washington.edu/

My C# code is as follows:

    // 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 responseData shows that the BUTTON hasn't been clicked when the program is run (I got this info from debugger of VS2010)


回答1:


it seems the url must be

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

since is the action of the form.



来源:https://stackoverflow.com/questions/10797995/send-http-post-request-through-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!