开发者

POSTing XML with non-ASCII characters

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜