gpt4 book ai didi

c# - 无法在 C# 中读取 UNICODE URL

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:59 24 4
gpt4 key购买 nike

以下代码将不起作用:

using System;
using System.IO;
using System.Net;
using System.Web;

namespace Proyecto_Prueba_04
{
class Program
{
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GetWebText(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

request.UserAgent = "A .NET Web Crawler";

WebResponse response = request.GetResponse();

Stream stream = response.GetResponseStream();

StreamReader reader = new StreamReader(stream);

string htmlText = reader.ReadToEnd();

return htmlText;
} // End of the GetWebText method.

/// <summary>
///
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
string urlPrueba = Uri.UnescapeDataString("http://?????????.??/");
Console.WriteLine("urlPrueba" + " = " + urlPrueba);

var encoded = HttpUtility.UrlPathEncode(urlPrueba);
Console.WriteLine("encoded" + " = " + encoded);

string codigoHTML = GetWebText(encoded);
Console.WriteLine("codigoHTML" + " = " + codigoHTML);

Console.ReadLine();
} // End of the Main method.
} // End of the Program class.
} // End of the Proyecto_Prueba_04 namespace.

我不明白我必须如何处理 UNICODE URL。

有什么想法吗?

谢谢。

最佳答案

您可以使用 IdnMapping类。

  string idn = "президент.рф";

IdnMapping mapping = new IdnMapping();
string asciiIdn = mapping.GetAscii(idn);
Console.WriteLine(asciiIdn);

var text = GetWebText("http://" + asciiIdn);
Console.WriteLine(text);

关于c# - 无法在 C# 中读取 UNICODE URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12381578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com