diff --git a/src/DohClient.cs b/src/DohClient.cs index 1df6f69..d6e8d1c 100644 --- a/src/DohClient.cs +++ b/src/DohClient.cs @@ -121,9 +121,15 @@ public override async Task QueryAsync( new CancellationTokenSource(Timeout).Token); // Post the request. - var content = new ByteArrayContent(request.ToByteArray()); - content.Headers.ContentType = new MediaTypeHeaderValue(DnsWireFormat); - var httpResponse = await HttpClient.PostAsync(ServerUrl, content, cts.Token); + HttpResponseMessage httpResponse; + using (var ms = new MemoryStream()) + { + request.Write(ms); + ms.Position = 0; + var content = new StreamContent(ms); + content.Headers.ContentType = new MediaTypeHeaderValue(DnsWireFormat); + httpResponse = await HttpClient.PostAsync(ServerUrl, content, cts.Token); + } // Check the HTTP response. httpResponse.EnsureSuccessStatusCode(); diff --git a/src/DotClient.cs b/src/DotClient.cs index cdf804c..78d67c1 100644 --- a/src/DotClient.cs +++ b/src/DotClient.cs @@ -267,17 +267,17 @@ byte[] BuildRequest(Message request) paddingOption.Padding = new byte[need]; }; - var udpRequest = request.ToByteArray(); - byte[] length = BitConverter.GetBytes((ushort)udpRequest.Length); - if (BitConverter.IsLittleEndian) + using (var tcpRequest = new MemoryStream()) { - Array.Reverse(length); + tcpRequest.WriteByte(0); // two byte length prefix + tcpRequest.WriteByte(0); + request.Write(tcpRequest); // udpRequest + var length = (ushort)(tcpRequest.Length - 2); + tcpRequest.Position = 0; + tcpRequest.WriteByte((byte) (length >> 8)); + tcpRequest.WriteByte((byte) (length)); + return tcpRequest.ToArray(); } - var tcpRequest = new byte[length.Length + udpRequest.Length]; - length.CopyTo(tcpRequest, 0); - udpRequest.CopyTo(tcpRequest, length.Length); - - return tcpRequest; } ///