如何强制 RestRequest 和 RestResponse 类使用“windows-1251”编码?
我的任务是以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档这里
所以:
From the documentation here
So: