Skip to content

Commit 4020f2d

Browse files
authored
Merge pull request #247 from qiniu/fix/stream-write-length
fix http request writing may lost some data with utf8 encoding
2 parents 5721cd5 + c31e046 commit 4020f2d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Qiniu/Http/HttpManager.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Specialized;
44
using System.Text;
55
using System.IO;
6+
using System.Linq;
67
using System.Net;
78
using Qiniu.Util;
89

@@ -633,7 +634,8 @@ public HttpResult PostJson(string url, string data, string token, bool binaryMod
633634
wReq.AllowWriteStreamBuffering = true;
634635
using (Stream sReq = wReq.GetRequestStream())
635636
{
636-
sReq.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
637+
byte[] utf8Data = Encoding.UTF8.GetBytes(data);
638+
sReq.Write(utf8Data, 0, utf8Data.Length);
637639
sReq.Flush();
638640
}
639641
}
@@ -750,7 +752,8 @@ public HttpResult PostText(string url, string data, string token, bool binaryMod
750752
wReq.AllowWriteStreamBuffering = true;
751753
using (Stream sReq = wReq.GetRequestStream())
752754
{
753-
sReq.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
755+
byte[] utf8Data = Encoding.UTF8.GetBytes(data);
756+
sReq.Write(utf8Data, 0, utf8Data.Length);
754757
sReq.Flush();
755758
}
756759
}
@@ -864,16 +867,14 @@ public HttpResult PostForm(string url, Dictionary<string, string> kvData, string
864867

865868
if (kvData != null)
866869
{
867-
StringBuilder sbb = new StringBuilder();
868-
foreach (var kv in kvData)
869-
{
870-
sbb.AppendFormat("{0}={1}&", Uri.EscapeDataString(kv.Key), Uri.EscapeDataString(kv.Value));
871-
}
870+
string data = string.Join("&",
871+
kvData.Select(kvp => Uri.EscapeDataString(kvp.Key) + "=" + Uri.EscapeDataString(kvp.Value)));
872872

873873
wReq.AllowWriteStreamBuffering = true;
874874
using (Stream sReq = wReq.GetRequestStream())
875875
{
876-
sReq.Write(Encoding.UTF8.GetBytes(sbb.ToString()), 0, sbb.Length - 1);
876+
byte[] utf8Data = Encoding.UTF8.GetBytes(data);
877+
sReq.Write(utf8Data, 0, utf8Data.Length);
877878
sReq.Flush();
878879
}
879880
}
@@ -990,7 +991,8 @@ public HttpResult PostForm(string url, string data, string token, bool binaryMod
990991
wReq.AllowWriteStreamBuffering = true;
991992
using (Stream sReq = wReq.GetRequestStream())
992993
{
993-
sReq.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
994+
byte[] utf8Data = Encoding.UTF8.GetBytes(data);
995+
sReq.Write(utf8Data, 0, utf8Data.Length);
994996
sReq.Flush();
995997
}
996998
}

0 commit comments

Comments
 (0)