RestSharp没有UTF-8编码请求

时间:2015-04-30 01:46:10

标签: c# .net encoding utf-8 restsharp

我正试图用一个孤独的参数发布请求,如下:

var client = new RestClient("http://www.fluff.com");
var request = new RestRequest("whatever", Method.POST);
request.AddParameter("param", "Оксана");
client.Execute(request);

这导致以下请求,注意一堆编码的问号:

POST http://www.fluff.com/whatever HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/105.0.1.0
Content-Type: application/x-www-form-urlencoded
Host: www.fluff.com
Content-Length: 24
Accept-Encoding: gzip, deflate

param=%3F%3F%3F%3F%3F%3F

想象一下当接收者得到那些问号时的悲伤......

如何让RestSharp将主体正确编码为UTF-8,或者如何以RestSharp友好的方式发送请求以使她不会丢失数据?

1 个答案:

答案 0 :(得分:2)

Christer,这是Content-Type: application/x-www-form-urlencoded的标准编码,它使用ISO-8859-1作为默认值。如果您特别想告诉服务器期望UTF-8,您可以在; charset=UTF-8末尾添加Content-Type: application/x-www-form-urlencoded ; charset=UTF-8。但是,您有责任确保您发布的数据真的以UTF-8编码。

或者如果你想在“application / json”中这样做,你可以用这种方式设置内容类型(我是从http://itanex.blogspot.com/2012/02/restsharp-and-advanced-post-requests.html得到的):

request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);

您还可以使用multipart/form-data<form action="YOUR_ACTION_NAME_HERE" method="post" enctype="multipart/form-data">