Cómo enviar una solicitud HTTP Post a un socket usando ASP.net C #

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

Aquí está mi código.

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.

preguntado el 01 de febrero de 12 a las 04:02

¿Qué es? socket? Is this supposed to be Socket (como en System.Net.Sockets.Socket? Also if you're working with Http then the preferred method would be to use the HttpClient clase. -

Either HttpClient, or maybe WebClient or WebRequest. Definitely not a socket. -

1 Respuestas

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.");     
}

Espero que esto ayude..

Respondido 01 Feb 12, 09:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.