Esempio n. 1
0
        private void BeginReceiveCallbackMethod(IAsyncResult result)
        {
            // we get the request that has been passed as state
            // and cast it back to its original class
            MyHttpRequest request = (MyHttpRequest)result.AsyncState;

            if (request == null)
            {
                return;
            }
            try
            {
                int        noBytes    = request.socket.EndReceive(result);
                HttpParser httpParser = new HttpParser();
                request.content.Append(Encoding.ASCII.GetString(request.buffer, 0, noBytes));

                if (!httpParser.ContainsHeader(request.content.ToString()))
                {
                    request.socket.BeginReceive(request.buffer,
                                                0,
                                                MyHttpRequest.bufferSize,
                                                0,
                                                BeginReceiveCallbackMethod,
                                                request);
                }
                else
                {
                    string body          = httpParser.GetResponseBody(request.content.ToString());
                    int    contentLength = httpParser.GetContentLenght(request.content.ToString());

                    if (body.Length < contentLength)
                    {
                        request.socket.BeginReceive(request.buffer,
                                                    0,
                                                    MyHttpRequest.bufferSize,
                                                    0,
                                                    BeginReceiveCallbackMethod,
                                                    request);
                    }
                    else
                    {
                        request.isReceived.Set();
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
Esempio n. 2
0
        private void BeginReceiveCallbackMethod(IAsyncResult result)
        {
            // we get the request that has been passed as state
            // and cast it back to its original class
            MyHttpRequest request = (MyHttpRequest)result.AsyncState;

            if (request == null)
            {
                return;
            }
            try
            {
                int        noBytes    = request.socket.EndReceive(result);
                HttpParser httpParser = new HttpParser();
                request.content.Append(Encoding.ASCII.GetString(request.buffer, 0, noBytes));

                if (!httpParser.ContainsHeader(request.content.ToString()))
                {
                    request.socket.BeginReceive(request.buffer,
                                                0,
                                                MyHttpRequest.bufferSize,
                                                0,
                                                BeginReceiveCallbackMethod,
                                                request);
                }
                else
                {
                    string body          = httpParser.GetResponseBody(request.content.ToString());
                    int    contentLength = httpParser.GetContentLenght(request.content.ToString());

                    if (body.Length < contentLength)
                    {
                        request.socket.BeginReceive(request.buffer,
                                                    0,
                                                    MyHttpRequest.bufferSize,
                                                    0,
                                                    BeginReceiveCallbackMethod,
                                                    request);
                    }
                    else
                    {
                        Console.WriteLine("Identificator " + request.identificator +
                                          ": Socket Connection for host=" + request.host +
                                          " received response!!!");
                        string[] content = request.content.ToString().Split('\r', '\n');
                        string   cont    = "";
                        foreach (string line in content)
                        {
                            cont += line + "\n";
                        }
                        Console.WriteLine("Identificator: " + request.identificator + "\n" + cont);
                        request.socket.Shutdown(SocketShutdown.Both);
                        request.socket.Close();
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }