Skip to content

Commit

Permalink
Fixed an issue with incorrect request body length due to multi-byte c…
Browse files Browse the repository at this point in the history
…haracters
  • Loading branch information
abjerner committed Feb 4, 2019
1 parent 6650383 commit f0561ab
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Skybrud.Essentials.Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,17 @@ public virtual IHttpResponse GetResponse(Action<HttpWebRequest> callback) {

// Handle various POST scenarios
if (!String.IsNullOrWhiteSpace(Body)) {

// Get the bytes for the request body
byte[] bytes = Encoding.UTF8.GetBytes(Body);

// Set the length of the request body
SetRequestContentLength(request, Body.Length);
SetRequestContentLength(request, bytes.Length);

// Write the body to the request stream
Task<Stream> hest = request.GetRequestStreamAsync();
using (Stream stream = hest.Result) {
stream.Write(Encoding.UTF8.GetBytes(Body), 0, Body.Length);
stream.Write(bytes, 0, Body.Length);
}

} else if (Method == HttpMethod.Post || Method == HttpMethod.Put || Method == HttpMethod.Patch || Method == HttpMethod.Delete) {
Expand Down Expand Up @@ -308,17 +311,20 @@ public virtual IHttpResponse GetResponse(Action<HttpWebRequest> callback) {
// Convert the POST data to an URL encoded string
string dataString = PostData.ToString();

// Get the bytes for the request body
byte[] bytes = Encoding.UTF8.GetBytes(dataString);

// Set the content type
request.ContentType = "application/x-www-form-urlencoded";

// Set the length of the request body
SetRequestContentLength(request, dataString.Length);
SetRequestContentLength(request, bytes.Length);

// Write the body to the request stream
Task<Stream> hest = request.GetRequestStreamAsync();
hest.Wait();
using (Stream stream = hest.Result) {
stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
stream.Write(bytes, 0, bytes.Length);
}

}
Expand Down

0 comments on commit f0561ab

Please sign in to comment.