Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从.NET应用程序发布表单_C#_Http_Post - Fatal编程技术网

C# 从.NET应用程序发布表单

C# 从.NET应用程序发布表单,c#,http,post,C#,Http,Post,我不熟悉http的东西,但我怎样才能向网站提交数据呢?有一个提交按钮,我想“按下”从控制台应用程序。这不是我自己的网站 这是页面源的一部分,不确定是否有任何相关性: <form action="rate.php" method="post"> 我查看了HttpWebRequest类,但不知道需要填写哪些属性 抱歉,我说得太模糊了,但我不熟悉http。您可以查看codeproject这是MSDN的c/p // Create a request using a URL th

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

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

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

我查看了HttpWebRequest类,但不知道需要填写哪些属性


抱歉,我说得太模糊了,但我不熟悉http。

您可以查看codeproject

这是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 ();


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

这里可以找到一个灵活且易于使用的示例:

-1因为代码中没有using语句-无论它是否来自MSDN。@John:省略using语句是一种常见做法。请为真正误导性的内容保留你的反对票。这个答案的作者不应该因为试图帮助别人而受到惩罚。@Wim:我不记得上一次我做了什么事情,因为你让我这么做。哦,等等:从来没有发生过。@Wim:这是导致新开发人员泄漏资源的常见做法,同时还有其他常见的错误异常处理做法。我把我的反对票留给那些让剪切粘贴开发者从坏习惯开始的人。@John,我同意Wim的观点,它确实会引起评论,但反对票太过分了
    // 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 ();