郁闷啊,使用WebRequest时有中文,得不到要的内容我把地址栏内已编码过的字串放进去就能得到内容请问在WINFORM下怎么对字串进行URL编码很急,哪位大哥知道,告诉小弟啊

解决方案 »

  1.   

    使用 System.Uri.HexEscape() 方法
      

  2.   

    另外, System.Web.HttpUtility.UrlEncode() 也可以用.
      

  3.   

    谢谢,我先试试System.Uri.HexEscape() 
    System.Web.HttpUtility.UrlEncode()好像在Winform下不能使用啊
      

  4.   

    555,有错
    代码:
    string src = "张韶涵";

    char[] tes = src.ToCharArray();
    src = "";
    foreach(char tt in tes)
    {
    //src += System.Uri.HexEscape(tt).ToString();
    MessageBox.Show(System.Uri.HexEscape(tt).ToString());
    }
    错误:
    有关调用实时(JIT)调试而不是此对话框的详细信息,
    请参阅此消息的结尾。************** 异常文本 **************
    System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
    参数名: character
       at System.Uri.HexEscape(Char character)
       at WindowsApplication3.Form1.Start_Click(Object sender, EventArgs e) in d:\ewebapp\edownload\edownload\windowsapplication3\form1.cs:line 456
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ************** 已加载的程序集 **************
    mscorlib
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
    ----------------------------------------
    WindowsApplication3
        程序集版本: 1.0.2094.41375
        Win32 版本: 1.0.2094.41375
        基本代码: file:///D:/eWebapp/eDownLoad/eDownLoad/WindowsApplication3/bin/Debug/WindowsApplication3.exe
    ----------------------------------------
    System.Windows.Forms
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
    ----------------------------------------
    System
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
    ----------------------------------------
    System.Drawing
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
    ----------------------------------------
    System.Xml
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
    ----------------------------------------
    mscorlib.resources
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
    ----------------------------------------
    System.Windows.Forms.resources
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///d:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
    ----------------------------------------************** JIT 调试 **************
    计算机的配置文件(machine.config)的 
     system.windows.forms 节中必须设置 jitDebugging 值。
    编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n    <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
      

  5.   

    using System.Windows.Forms;public class Test : Form
    {
      static void Main()
      {
        string src = "张韶涵";
        string msg = System.Web.HttpUtility.UrlEncode(src);
        MessageBox.Show(msg);
      }
    }
      

  6.   

    嗯好了,引用了System.Web名称空间后就可以使用System.Web.HttpUtility.UrlEncode
    但我在想,winform本身也可以做到的,
    url中文编辑应该就是把汉字转字16进制吧.