打开发送邮件的URL

时间:2010-10-16 14:29:10

标签: .net winforms

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

像这个html函数的东西。

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

我想要上面提到的html功能,打开网址,发布内容并在打开的页面中查看结果。我不需要网页休息。谢谢

3 个答案:

答案 0 :(得分:4)

您希望发布如下的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

答案 1 :(得分:3)

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

答案 2 :(得分:2)

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

有关使用WebClient发布表单数据的详情,请参阅thisthis问题。