Convert utf8 to 1251

So, the problem is as follows. I have to get the information from the online form, and then send it again (modified). The page is in windows-1251, and in my request I get it with:

{ // used to build entire input StringBuilder sb = new StringBuilder(); // used on each read operation byte[] buf = new byte[8192]; // prepare the web page we will be asking for HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); // execute the request HttpWebResponse response = (HttpWebResponse) request.GetResponse(); // we will read data via the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { // fill the buffer with data count = resStream.Read(buf, 0, buf.Length); // make sure we read some data if (count != 0) { // translate from bytes to ASCII text tempString = Encoding.GetEncoding(1251).GetString(buf, 0, count); // continue building the string sb.Append(tempString); } } while (count > 0); // any more data to read? // print out page source // Console.WriteLine(sb.ToString()); return sb.ToString(); 

So, after that I break the page and get my data into an array, and I need to send it to the site again, but it should be encoded. I use System.Net.WebUtility.UrlEncode for coding, but I get completely different results from my script and from firefox (I got the original using tamper data). Therefore, any help or ideas are appreciated. BTW after I received the encoded data (from a script), I tried it on the Internet http://www.url-encode-decode.com/ and only UTF-8 returned the correct result, so I assume that I need convert it to 1251, and I tried to do it, but it was still gibberish and completely different than what was supposed to be ... So please, if you can help me, it will be great ...: )

Edit:

 Expected: %E3%F0%E0%E4+%D1%EE%F4%E8%FF Actual: %D0%B3%D1%80%D0%B0%D0%B4+%D0%A1%D0%BE%D1%84%D0%B8%D1%8F f2[11] =   - f2 {string[180]} string[] [0] "127.0.0.1" string [1] "1348247257" string [2] "1n132135075318435" string [3] "151669n1" string [4] "0" string [5] null string [6] null string [7] null string [8] "2" string [9] "12871449760521116" string [10] "14" string [11] " " string [12] "" string [13] "42.707984,23.41341,0~.  " string [14] "42.7100653,23.4274006,1" string 
+4
source share
1 answer

The answer (achieved through comments) was to replace WebUtility.UrlEncode with HttpUtility.UrlEncode‌​ This method has an overload that takes encoding as a parameter.

+4
source

Source: https://habr.com/ru/post/1435812/


All Articles