【问题标题】:Can Not Read UNICODE URL in C#无法在 C# 中读取 UNICODE URL
【发布时间】:2012-09-12 05:11:35
【问题描述】:

以下代码不起作用:

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。

有什么想法吗?

谢谢。

【问题讨论】:

  • 您在此处看到的 URL 为 ????是一个俄语 URL,是这个:президент.рф
  • 如果您的平台不能为您处理,您需要转换为 IDNA Punycode。但是,如果您的源文件不包含采用合理编码的 URL,那么当然不会有任何效果。

标签: c# unicode web-crawler idn


【解决方案1】:

您可以使用IdnMapping 类。

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

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

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

【讨论】:

    【解决方案2】:

    使用 System.Uri 代替字符串中的 url。

    这行得通,我试过了:

    using System;
    using System.IO;
    using System.Net;
    using System.Web;
    
    namespace Proyecto_Prueba_04
    {
    internal class Program
    {
        public static string GetWebText(Uri uri)
        {
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.UserAgent = "A .NET Web Crawler";
    
            string htmlText = null;
            using (var response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            htmlText = reader.ReadToEnd();
                        }
                    }
                }
            }
    
            return htmlText;
        }
    
        public static void Main(string[] args)
        {
            const string urlPrueba = "http://президент.рф/";
            var uri = new Uri(urlPrueba);
    
            Console.WriteLine("urlPrueba" + " = " + uri.AbsoluteUri);
    
            string codigoHTML = GetWebText(uri);
            Console.WriteLine("codigoHTML" + " = " + codigoHTML);
    
            Console.ReadLine();
        }
    }
    
    } 
    

    【讨论】:

    • 请添加一些usings。
    猜你喜欢
    • 2017-02-22
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多