private async Task SocketBeginSend(MyHttpRequest request) { HttpParser httpParser = new HttpParser(); byte[] data = Encoding.ASCII.GetBytes(httpParser.CreateGetRequestString(request.host, request.endpoint)); request.socket.BeginSend(data, 0, data.Length, 0, BeginSendCallbackMethod, request); await Task.FromResult <object>(request.isSent.WaitOne()); }
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()); } }
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()); } }