public HttpWorker(IHttpWorkerClient client, Uri uri, Dictionary <string, string> headers, HttpMethod httpMethod = HttpMethod.Get, byte[] data = null) { _client = client; _buffer = new Memory <byte>(new byte[8192]); var headersString = string.Empty; var contentLength = data?.Length ?? 0; Lock.Wait(); if (headers == null) { headers = new Dictionary <string, string>(); } headers.AddTraceId(); if (headers != null && headers.Any()) { headersString = string.Concat(headers.Select(h => "\r\n" + h.Key.Trim() + ": " + h.Value.Trim())); } Lock.Release(); var request = Encoding.UTF8.GetBytes($"{httpMethod.ToString().ToUpper()} {uri.PathAndQuery} HTTP/1.1\r\nAccept-Encoding: gzip, deflate, sdch\r\nHost: {uri.Host}\r\nContent-Length: {contentLength}{headersString}\r\n\r\n"); if (data != null) { var tmpRequest = new byte[request.Length + data.Length]; Buffer.BlockCopy(request, 0, tmpRequest, 0, request.Length); Buffer.BlockCopy(data, 0, tmpRequest, request.Length, data.Length); request = tmpRequest; } _request = request.AsMemory(); }
public HttpWorker(IHttpWorkerClient client, Uri uri, HttpMethod httpMethod = HttpMethod.Get, Dictionary <string, string> headers = null, byte[] data = null) { _client = client; _buffer = new byte[8192]; _responseType = ResponseType.Unknown; var headersString = string.Empty; var contentLength = data?.Length ?? 0; if (headers != null && headers.Any()) { headersString = string.Concat(headers.Select(h => "\r\n" + h.Key.Trim() + ": " + h.Value.Trim())); } _request = Encoding.UTF8.GetBytes($"{httpMethod.ToString().ToUpper()} {uri.PathAndQuery} HTTP/1.1\r\nAccept-Encoding: gzip, deflate, sdch\r\nHost: {uri.Host}\r\nContent-Length: {contentLength}{headersString}\r\n\r\n"); if (data != null) { var tmpRequest = new byte[_request.Length + data.Length]; Buffer.BlockCopy(_request, 0, tmpRequest, 0, _request.Length); Buffer.BlockCopy(data, 0, tmpRequest, _request.Length, data.Length); _request = tmpRequest; } }