Reputation: 47
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.
Upvotes: 2
Views: 4474
Reputation: 3770
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..
Upvotes: 2