我正在尝试修补RestSharp以使其能够POST
使用非 ASCII 字符作为POST
请求正文的 XML。
以下是它的编写方式:
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
是一个字符串,当服务器实际尝试解析请求时,所有非 ASCII 字符都会变成???
.
现在,我执行以下操作:
var encoding = Encoding.UTF8;
webRequest.ContentLength = encoding.GetByteCount(RequestBody);
var requestStream = webRequest.GetRequestStream();
using (var writer = new StreamWriter(requestStream, encoding)) {
writer.Write(RequestBody);
}
但它会抛出“IOException
在Stream.Dispose()
写入所有字节之前无法关闭流”。
如何发布此 XML?