简体   繁体   English

读取HTML页面上的C#编码

[英]C# Encoding on Reading HTML Page

I am visiting a url with my program and then having the program read the resulting html. 我正在用程序访问一个url,然后让程序读取生成的html。

    string URI = "http://www.testwebsite.com/submit?q=+mydata";
    string myParameters = "";

                    using (WebClient wc = new WebClient())
                    {
                        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                        HtmlResult = wc.UploadString(URI, myParameters);

                    }

This perfectly allows me to post my data, have the website read it and then the website replies. 这完全可以让我发布我的数据,让网站读取它,然后网站进行回复。 This all works great! 这一切都很棒!

HOWEVER! 然而!

It only properly reads english characters. 它只能正确读取英文字符。 I need the program to properly read korean characters too. 我也需要该程序来正确阅读韩文字符。 I have tested and seen that the website properly sends korean characters when visiting in a browser. 我进行了测试,发现该网站在使用浏览器访问时可以正确发送朝鲜语字符。 However my C# program just gets a bunch of garbage data if it is not english characters. 但是,如果不是英语字符,我的C#程序只会得到一堆垃圾数据。

Any ideas? 有任何想法吗?

您可以尝试设置WebClient编码并在请求中发送字符集。

using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=utf-8"; HtmlResult = wc.UploadString(URI, myParameters); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM