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 writer = 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?