I'm trying to patch RestSharp for it to be able to POST
XMLs with non-ASCII characters as POST
request body.
Here's how it gets written:
private void WriteRequestBody(HttpWebRequest webRequest) {
if (HasBody) {
webRequest.ContentLength = RequestBody.Length;
var requestStream = webRequest.GetRequestStream();
using (var w开发者_C百科riter = new StreamWriter(requestStream, Encoding.ASCII)) {
writer.Write(RequestBody);
}
}
}
RequestBody
is a string and when server actually tries to parse the request, all non-ASCII characters turn into ???
.
Now, I do the following:
var encoding = Encoding.UTF8;
webRequest.ContentLength = encoding.GetByteCount(RequestBody);
var requestStream = webRequest.GetRequestStream();
using (var writer = new StreamWriter(requestStream, encoding)) {
writer.Write(RequestBody);
}
But it throws IOException
on Stream.Dispose()
saying "Cannot close stream until all bytes are written."
How do I post this XML?
I haven't used RestSharp but looking explanation my guess is that the payload's ContentLength does not match the internal-string. XML uses UTF-8 escapes, so the payload could become larger. So on original string the representation could Content-Length could differ.
Maybe you calculate the Content-Length at a wrong place?
精彩评论