从.NET应用程序发布表单

时间:2010-05-16 04:25:46

标签: c# http post

我不熟悉http的内容,但我怎样才能将数据提交到网站?有一个提交按钮,我想从控制台应用程序“按下”。这不是我自己的网站。

这是页面来源的一部分,不确定它是否具有任何相关性:

<form action="rate.php" method="post">

我查看了HttpWebRequest类,但我不熟悉需要填写的属性。

抱歉,我很模糊,但我不熟悉http。

4 个答案:

答案 0 :(得分:3)

这是来自MSDN的c / p。

    // 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);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream ();
    // Write the data to the request stream.
    dataStream.Write (byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close ();
    // Get the response.
    WebResponse response = request.GetResponse ();
    // Display the status.
    Console.WriteLine (((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream ();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader (dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd ();
    // Display the content.
    Console.WriteLine (responseFromServer);
    // Clean up the streams.
    reader.Close ();
    dataStream.Close ();
    response.Close ();

link to page

这个过程非常简单,但你需要先弄清楚你需要发送什么以及可能需要的任何其他特殊编码/ cookies /等等。我建议您对Firefox使用Fiddler和/或Firebug。您可以通过网页查看工作请求中发生的所有事情,然后您可以在应用中模仿相同的行为。

答案 1 :(得分:0)

答案 2 :(得分:0)

可在此处找到灵活且易于使用的示例:C# File Upload with form fields, cookies and headers

答案 3 :(得分:-3)

Response.Write("hello!");
Response.End();