如何强制 RestRequest 和 RestResponse 类使用“windows-1251”编码?

发布于 2024-12-09 08:18:49 字数 1821 浏览 0 评论 0原文

我的任务是以 XML 格式(字符集 =“windows-1251”)发送请求并接收响应。 当我使用 HttpWebRequest 和 HttpWebResponse 类(代码片段 1)时,它工作正常。 但是 RestRequest 和 RestResponse 类存在问题(代码片段 2)。 client.Execute(req) 代码返回带有 ErrorException = {“输入字符串格式不正确。”}的响应。 我想,问题是 RestSharp 的类无法识别“windows-1251”编码。如何强制他们使用“windows-1251”编码?

HttpWebResponse 的响应对象类型的状态:

RestResponse 的响应对象类型的状态:

代码片段 1:

byte[] bytes = Encoding.GetEncoding(1251).GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1251));
    resultXML = sr.ReadToEnd();
    sr.Close();
}

代码片段 2:

private T ExecuteRequest<T>(string resource, RestSharp.Method httpMethod, 
    string bodyXML = null) where T : new()
{
    RestClient client = new RestClient(this.BaseUrl);
    RestRequest req = new RestRequest(resource, httpMethod);
    req.AddParameter("text/xml", bodyXML, ParameterType.RequestBody);
    RestResponse<T> resp = client.Execute<T>(req);
    return resp.Data;
}

XML 请求示例:

<?xml version="1.0" encoding="windows-1251"?>
<digiseller.request>
  <id_seller>1</id_seller>
  <order></order>
</digiseller.request>

My task is to send request and receive response in XML format (charset = "windows-1251").
It works correct when I use HttpWebRequest and HttpWebResponse classes (code snippet 1).
But there is an issue with the RestRequest and RestResponse classes (code snippet 2).
The client.Execute(req) code returns response with ErrorException = {"Input string was not in a correct format."}.
I suppose, problem is that RestSharp's classes cannot recognize "windows-1251" encoding. How to force them use "windows-1251" encoding?

State of the response obect type of HttpWebResponse:


State of the response obect type of RestResponse:

Code snippet 1:

byte[] bytes = Encoding.GetEncoding(1251).GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1251));
    resultXML = sr.ReadToEnd();
    sr.Close();
}

Code snippet 2:

private T ExecuteRequest<T>(string resource, RestSharp.Method httpMethod, 
    string bodyXML = null) where T : new()
{
    RestClient client = new RestClient(this.BaseUrl);
    RestRequest req = new RestRequest(resource, httpMethod);
    req.AddParameter("text/xml", bodyXML, ParameterType.RequestBody);
    RestResponse<T> resp = client.Execute<T>(req);
    return resp.Data;
}

Sample of the XML request:

<?xml version="1.0" encoding="windows-1251"?>
<digiseller.request>
  <id_seller>1</id_seller>
  <order></order>
</digiseller.request>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2024-12-16 08:18:49

从文档这里

请求正文

如果设置了该参数,其值将作为消息体发送
要求。仅接受一个 RequestBody 参数 - 第一个。

参数的名称将用作 Content-Type 标头
请求。

所以:

request.AddParameter(new Parameter
{
    Name = "text/xml; charset=windows-1251",
    Type = ParameterType.RequestBody,
    Value = bodyXML
})

From the documentation here

RequestBody

If this parameter is set, its value will be sent as the body of the
request. Only one RequestBody Parameter is accepted – the first one.

The name of the parameter will be used as the Content-Type header for
the request.

So:

request.AddParameter(new Parameter
{
    Name = "text/xml; charset=windows-1251",
    Type = ParameterType.RequestBody,
    Value = bodyXML
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文