2012-08-08 11 views
12

Estoy tratando de hacer un HTTP request utilizando sockets. Mi código es el siguiente:C#: ¿Cómo ejecutar una solicitud HTTP usando sockets?

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
class test 
{ 
    public static void Main(String[] args) 
    { 
     string hostName = "127.0.0.1"; 
     int hostPort = 9887; 
     int response = 0; 

     IPAddress host = IPAddress.Parse(hostName); 
     IPEndPoint hostep = new IPEndPoint(host, hostPort); 
     Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

     sock.Connect(hostep); 

     string request_url = "http://127.0.0.1/register?id=application/vnd-fullphat.test&title=My%20Test%20App"; 
     response = sock.Send(Encoding.UTF8.GetBytes(request_url)); 
     response = sock.Send(Encoding.UTF8.GetBytes("\r\n")); 

     bytes = sock.Receive(bytesReceived, bytesReceived.Length, 0); 
     page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes); 
     Console.WriteLine(page); 
     sock.Close(); 
    } 
} 

Ahora, cuando ejecuto el código anterior no pasa nada mientras que cuando entro en mi request_url en el navegador recibo una notificación de Gruñido diciendo que Application Registered y la respuesta que recibo de navegador es

SNP/2.0/0/OK/556 

La respuesta que recibo de mi código es SNP/3.0/107/BadPacket.

Entonces, ¿qué hay de malo en mi código.

Snarl Request format specification

+1

¿Existe algún motivo por el que necesite utilizar Sockets en lugar de, digamos, 'System.Net.Http.HttpClient'? –

+2

o Webclient para ese asunto ... – steveg89

+0

No, no hay un motivo específico. En realidad, no tenía conocimiento de 'HttpCLient o Webclient'. Pero de todos modos, me gustaría entender qué es wrog con mi código anterior. – RanRag

Respuesta

7

Debe incluir contenidos de longitud y doble nueva línea al final para indicar final de cabecera.

var request = "GET /register?id=application/vnd-fullphat.test&title=My%20Test%20App HTTP/1.1\r\n" + 
    "Host: 127.0.0.1\r\n" + 
    "Content-Length: 0\r\n" + 
    "\r\n"; 

La especificación HTTP 1.1 se puede encontrar aquí: http://www.w3.org/Protocols/rfc2616/rfc2616.html

+0

Gracias amigo. Justo lo que estaba buscando. – kurt

1

Su solicitud no es correcta. De acuerdo con wikipedia, a HTTP Get request tiene que quedar así:

string request = "GET /register?id=application/vnd-fullphat.test&title=My%20Test%20App HTTP/1.1\r\nHost: 127.0.0.1\r\n"; 
+0

Todavía estoy recibiendo 'SNP/3.0/107/BadPacket' – RanRag

+3

Parece que necesita algo más en su solicitud, como Conexión, Aceptar o tal vez Aceptar codificación. Lo que PVitt mostró es una solicitud mínima, aunque eso podría no ser suficiente para su protocolo específico. Intente trabajar con WebClient como se sugiere arriba y vea si eso funciona para asegurarse de que la API funcione como debería. –

+0

De acuerdo con la respuesta aceptada, debe haber sido 'Content-Length'. – vapcguy

13

No sé nada de SNP. Su código es un poco confuso en la parte de recepción. He utilizado el siguiente ejemplo para enviar y leer la respuesta del servidor para una solicitud HTTP GET. Primero echemos un vistazo a la solicitud y luego examinemos la respuesta.

petición HTTP GET:

GET/HTTP/1.1 
Host: 127.0.0.1 
Connection: keep-alive 
Accept: text/html 
User-Agent: CSharpTests 

string - "GET/HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: keep-alive\r\nAccept: text/html\r\nUser-Agent: CSharpTests\r\n\r\n" 

servidor de cabecera de respuesta HTTP:

HTTP/1.1 200 OK 
Date: Sun, 07 Jul 2013 17:13:10 GMT 
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16 
Last-Modified: Sat, 30 Mar 2013 11:28:59 GMT 
ETag: \"ca-4d922b19fd4c0\" 
Accept-Ranges: bytes 
Content-Length: 202 
Keep-Alive: timeout=5, max=100 
Connection: Keep-Alive 
Content-Type: text/html 

string - "HTTP/1.1 200 OK\r\nDate: Sun, 07 Jul 2013 17:13:10 GMT\r\nServer: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16\r\nLast-Modified: Sat, 30 Mar 2013 11:28:59 GMT\r\nETag: \"ca-4d922b19fd4c0\"\r\nAccept-Ranges: bytes\r\nContent-Length: 202\r\nKeep-Alive: timeout=5, max=100\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\n\r\n" 

He omite, deliberadamente el cuerpo de la respuesta del servidor, porque ya sabemos que es exactamente 202 bytes, como se especifica por Content-Length en el encabezado de respuesta.

Un vistazo a la especificación HTTP revelará que un encabezado HTTP termina con una nueva línea vacía ("\ r \ n \ r \ n"). Entonces solo tenemos que buscarlo.

Veamos un código en acción. Suponga un socket variable de tipo System.Net.Sockets.Socket.

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
socket.Connect("127.0.0.1", 80); 
string GETrequest = "GET/HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: keep-alive\r\nAccept: text/html\r\nUser-Agent: CSharpTests\r\n\r\n"; 
socket.Send(Encoding.ASCII.GetBytes(GETrequest)); 

Hemos enviado la solicitud al servidor, recibamos y analizamos correctamente la respuesta.

bool flag = true; // just so we know we are still reading 
string headerString = ""; // to store header information 
int contentLength = 0; // the body length 
byte[] bodyBuff = new byte[0]; // to later hold the body content 
while (flag) 
{ 
// read the header byte by byte, until \r\n\r\n 
byte[] buffer = new byte[1]; 
socket.Receive(buffer, 0, 1, 0); 
headerString += Encoding.ASCII.GetString(buffer); 
if (headerString.Contains("\r\n\r\n")) 
{ 
    // header is received, parsing content length 
    // I use regular expressions, but any other method you can think of is ok 
    Regex reg = new Regex("\\\r\nContent-Length: (.*?)\\\r\n"); 
    Match m = reg.Match(headerString); 
    contentLength = int.Parse(m.Groups[1].ToString()); 
    flag = false; 
    // read the body 
    bodyBuff = new byte[contentLength]; 
    socket.Receive(bodyBuff, 0, contentLength, 0); 
} 
} 
Console.WriteLine("Server Response :"); 
string body = Encoding.ASCII.GetString(bodyBuff); 
Console.WriteLine(body); 
socket.Close(); 

Este es probablemente el peor método para hacer esto en C#, hay un montón de clases para manejar las peticiones y respuestas HTTP en .NET, pero aún si lo necesitas, funciona.

0

El requisito mínimo para una solicitud HTTP es "GET/HTTP/1.0 \ r \ n \ r \ n" (si se permite la eliminación del host). Pero en SNARL, debe ingresar la longitud del contenido (lo que más escuché).

Así,

 Socket sck = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); 

     sck.Connect(ip, port); 
     sck.Send(Encoding.UTF8.GetBytes("THE HTTP REQUEST HEADER")); 
     Console.WriteLine("SENT"); 
     string message = null; 
     byte[] bytesStored = new byte[ReceiveBufferSize]; 
     int k1 = sck.Receive(bytesStored); 
     for (int i = 0; i < k1; i++) 
     { 
      message = message + Convert.ToChar(bytesStored[i]).ToString(); 
     } 
     Console.WriteLine(message); // PRINT THE RESPONSE 

EDIT: Estoy profundamente lo siento por la pobre respuesta anterior. No se ha formulado y agregué la idea/solución principal de la respuesta (sin información directa). Además, agregué el código fuente.

He probado en varios sitios y funciona perfectamente. Si no funcionó, entonces debe solucionarlo agregando más encabezado (la solución más probable).

Cuestiones relacionadas