【问题标题】:Open Url sending POST打开 URL 发送 POST
【发布时间】:2010-10-16 22:58:09
【问题描述】:

如何在 .net 2 中打开网络浏览器并发送 POST

类似这个 html 函数的东西。

<form action="http:www.url.com/get" method="post">
  <input name="tt_2a" >
  <input type="submit" value="submit">

我想要一些类似于我上面提到的 html 函数的东西,打开一个 url,发布一些东西,然后在打开的页面中查看结果。我不需要网络响应。谢谢

【问题讨论】:

  • 不清楚你想要达到什么。您是在谈论 webforms(客户端还是服务器端?)、WPF 还是 winforms?
  • 我想用winforms打开Url发帖。
  • 在这种情况下,请用 winforms 标记您的问题,以便您获得相关答案和更多人关注。

标签: .net winforms


【解决方案1】:

你想像这样发布一个 WebRequest:

        // 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 ();

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

【讨论】:

    【解决方案2】:

    如果您想在幕后进行 POST(即不显示浏览器窗口/控件),这些链接可能会有所帮助:

    【讨论】:

      【解决方案3】:

      您可以在您的 winforms 应用程序中使用WebClient 来处理网页。

      有关使用WebClient 发布表单数据的更多信息,请参阅thisthis 问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-25
        • 2020-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        相关资源
        最近更新 更多