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 Dec 8, 2018
1 parent e7ddcf6 commit 79919bc
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Skybrud.Social.Core/Http/SocialHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,16 @@ public class SocialHttpRequest {
// 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, bytes.Length);
}

} else if (Method == SocialHttpMethod.Post || Method == SocialHttpMethod.Put || Method == SocialHttpMethod.Patch || Method == SocialHttpMethod.Delete) {
Expand All @@ -309,18 +312,21 @@ public class SocialHttpRequest {

// 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 79919bc

Please sign in to comment.