我正在开发我的项目,我是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的帮助。
答案 0 :(得分:2)
您可以使用类似下面的代码来使用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.");
}
希望这会有所帮助..