📜  C#| Uri.EscapeDataString(String)方法

📅  最后修改于: 2021-05-30 00:06:07             🧑  作者: Mango

Uri.EscapeDataString(String)方法用于将字符串转换为其转义的表示形式。

下面的程序说明了Uri.EscapeDataString(String)方法的用法

范例1:

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing address1
            string address1 = "http://www.contoso.com/index.htm#search";
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1);
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Escaped string is : http%3A%2F%2Fwww.contoso.com%2Findex.htm%23search

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Declaring and initializing address1
            string address1 = null;
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1);
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("stringToEscape can not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        catch (UriFormatException e) 
        {
            Console.WriteLine("Length of stringToEscape should"+
                          " not exceed from 32766 characters.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
stringToEscape can not be null
Exception Thrown: System.ArgumentNullException

示例3:对于UriFormatException

// C# program to demonstrate the
// Uri.EscapeDataString(String) Method
using System;
using System.Text;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing address1
            StringBuilder address1 = new StringBuilder("http://www.contoso.com/index.htm#search");
  
            // appending StringBuilder
            for (int i = 1; i <= 3000; i++)
                address1.Append("abcedfghijklmnopdjdjdjdjdjjddjjdjdj");
  
            // Converting a string to its 
            // escaped representation
            // using EscapeDataString() method
            string value = Uri.EscapeDataString(address1.ToString());
  
            // Displaying the result
            Console.WriteLine("Escaped string is : {0}", value);
        }
  
        catch (ArgumentNullException e)
       {
            Console.WriteLine("stringToEscape can not be null");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        catch (UriFormatException e)
        {
            Console.WriteLine("Length of stringToEscape should"+
                          " not exceed from 32766 characters.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Length of stringToEscape should not exceed from 32766 characters.
Exception Thrown: System.UriFormatException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.uri.escapedatastring?view=netstandard-2.1