gpt4 book ai didi

.net - 打开网址发送POST

转载 作者:行者123 更新时间:2023-12-04 17:54:00 24 4
gpt4 key购买 nike

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

类似于这个 html 函数。

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

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

最佳答案

你想像这样发布一个 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

关于.net - 打开网址发送POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3949280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com