2
我試圖修補RestSharp,因爲它能夠POST
具有非ASCII字符的XML作爲POST
請求正文。使用非ASCII字符發佈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);
}
但它Stream.Dispose()
拋出IOException
說
如何發佈此XML?