WebClient.DownloadString results in mangled characters due to encoding issues, but the browser is OK

前端 未结 1 1179
孤街浪徒
孤街浪徒 2020-11-28 09:58

The following code:

var text = (new WebClient()).DownloadString(\"http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20         


        
相关标签:
1条回答
  • 2020-11-28 10:47

    It's not lying. You should set the webclient's encoding first before calling DownloadString.

    using(WebClient webClient = new WebClient())
    {
    webClient.Encoding = Encoding.UTF8;
    string s = webClient.DownloadString("http://export.arxiv.org/api/query?search_query=au:Freidel_L*&start=0&max_results=20");
    }
    

    As for why your alternative isn't working, it's because the usage is incorrect. Its should be:

    System.Text.Encoding.UTF8.GetString()
    
    0 讨论(0)
提交回复
热议问题