Good afternoon, I use the xNet reverse library (convenient and well documented).

The essence of the problem: send a POST request multipart form-data to the site (encoding site Windows-1251). In the query parameters set:

request.CharacterSet = Encoding.GetEncoding("Windows-1251"); 

But in the end, the site receives a request in UTF-8 encoding.

I tried to process the text in this way:

 HttpHelper.UrlEncode("str", Encoding.GetEncoding("Windows-1251")); 

But unfortunately it turns out this:

 %C3%EE%F2%EE%E2+%EE%E1%F1%F3%E4%E8%F2%FC+%F1%F0%EE%EA%E8+%E8+%F6%E5%ED%F3.%0D%0A%0D%0A 

those. in multipart form-data, form-urlencoded does not work on the site side.

How can you get out of the situation? Here is an example code for how to send a request:

 using xNet.Net; using xNet.Collections; using xNet.Security; using xNet.Text; using xNet.Threading; using (var request = new xNet.Net.HttpRequest()) { request.UserAgent = xNet.Net.HttpHelper.FirefoxUserAgent(); request.AllowAutoRedirect = true; request.KeepAlive = true; request.Referer = @"http://site.ru/"; request.CharacterSet = System.Text.Encoding.GetEncoding("Windows-1251"); //Задаем куки request.Cookies = cookie; //Параметры POST запроса var reqParams = new xNet.Collections.RequestParams(); reqParams["in_office"] = "0"; reqParams["cost"] = Cst; reqParams["term"] = Dt; reqParams["msg_body"] = xNet.Net.HttpHelper.UrlEncode( Text.Trim(), System.Text.Encoding.GetEncoding("Windows-1251")); reqParams["att_file"] = string.Empty; reqParams["cmd"] = "msg_insert"; reqParams["topic"] = Topic.Trim(); await System.Threading.Tasks.Task.Run(() => { @out = request.Post(@"http://site.ru/dis/", reqParams).ToString(); }); //Возвращаем куки cookie = request.Cookies; reqParams.Clear(); //Возвращаем результат return @out ; } 
  • Where did you get request.CharacterSet ? There is no such thing in the HttpRequest class. - VladD
  • I use the third-party library xNet (convenient and well documented), here is the link to it and the source: github.com/X-eng/xNet - Alexis
  • That is, it is not System.Web.HttpRequest ? Then ask the library developers, we don’t know what their meaning is. - VladD
  • True, this is not System.Web.HttpRequest. Unfortunately, the developer could not be contacted in a few days. Sorry for misleading. - Alexis
  • one
    @ z668: maybe someone here had experience with their library. It seems to me that it makes sense to specify full names in the code for types, with namespaces, to avoid confusion. - VladD

1 answer 1

You can solve the problem like this:

 request.AddField("msg_body", Encoding.GetEncoding("Windows-1251").GetBytes("строка")); 

Instead of bytes, you can pass a string, but there is a small bug, which always uses the UTF-8 encoding.

  • As I understand you - the author of this library. Fix a bug with github encoding? It hurts the library is like. - Alexis
  • one
    Already fixed and filled. - Xrus