如何使用 ASP.net C# 将 HTTP Post 请求发送到套接字

发布于 2025-01-01 01:52:19 字数 502 浏览 1 评论 0原文

我正在开发我的项目,并且我是 ASP.NET 的新手。

我想在点击按钮时向套接字发送 HTTP post 请求,

这是我的代码。

protect void Button1_click(object sender, EventArgs e)
{
   socket clientSocket = new Socket (addressFamily.InterNetwork, SocketType.Stream, Protocol.TCP);
   clientSocket.Connect(new IPEndPont.Parse("192.168.1.1", 5550));

   A = "1"; // i want to send this variable using HTTP post request

   clientSocket.Send(Encoding.UTF8.Getbytes(A));

   clientSocket.Close();
}

tnx 提供帮助。

I'm developing on my project and I'm new to ASP.NET.

I want to send HTTP post request to a socket when I hit a button

here is my code.

protect void Button1_click(object sender, EventArgs e)
{
   socket clientSocket = new Socket (addressFamily.InterNetwork, SocketType.Stream, Protocol.TCP);
   clientSocket.Connect(new IPEndPont.Parse("192.168.1.1", 5550));

   A = "1"; // i want to send this variable using HTTP post request

   clientSocket.Send(Encoding.UTF8.Getbytes(A));

   clientSocket.Close();
}

tnx for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找回味觉 2025-01-08 01:52:19

您可以使用类似下面的代码来使用 POST 方法发送 HTTP 请求...

将自动创建一个套接字(服务器 + 端口)来处理服务器上的数据以处理请求。

WebRequest request = WebRequest.Create(url);
request.Method = "POST";


string postData = "Data to post here"

byte[] post = Encoding.UTF8.GetBytes(postData); 

//Set the Content Type     
request.ContentType = "application/x-www-form-urlencoded";     
request.ContentLength = post.Length;      
Stream reqdataStream = request.GetRequestStream();     
// Write the data to the request stream.     
reqdataStream.Write(post, 0, post.Length);      
reqdataStream.Close();      
// If required by the server, set the credentials.     
request.Credentials = CredentialCache.DefaultCredentials;     

WebResponse response = null;     
try     
{
    // Get the response.         
    response = request.GetResponse();      
}   
catch (Exception ex)     
{         
    Response.Write("Error Occured.");     
}

希望这有帮助..

You could use something like the code below to send an HTTP request using POST Method...

A socket (Server + Port) will be automatically created to handle the data on the server to process the request.

WebRequest request = WebRequest.Create(url);
request.Method = "POST";


string postData = "Data to post here"

byte[] post = Encoding.UTF8.GetBytes(postData); 

//Set the Content Type     
request.ContentType = "application/x-www-form-urlencoded";     
request.ContentLength = post.Length;      
Stream reqdataStream = request.GetRequestStream();     
// Write the data to the request stream.     
reqdataStream.Write(post, 0, post.Length);      
reqdataStream.Close();      
// If required by the server, set the credentials.     
request.Credentials = CredentialCache.DefaultCredentials;     

WebResponse response = null;     
try     
{
    // Get the response.         
    response = request.GetResponse();      
}   
catch (Exception ex)     
{         
    Response.Write("Error Occured.");     
}

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文