如何在Windows Form中实现URL Encoding/Decoding?

作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-7-24 17:32:07

如果你想在Windows Forms中实现URL Encoding,HttpUtility类有一个shared (static)方法实现对字符串的URL 编码。下面就是一个例子:【注意要引用System.Web】

using System.Reflection.Metadata;
using System.Web;
using System.Windows.Controls;

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Web

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
    MyBase.New()
    '该调用是 Windows 窗体设计器所必需的。
    InitializeComponent()
    '在 InitializeComponent() 调用之后添加任何初始化
End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
    If Not (components Is Nothing) Then
        components.Dispose()
    End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
FriEnd WithEvents TextBox1 As System.Windows.Forms.TextBox
FriEnd WithEvents Button1 As System.Windows.Forms.Button
FriEnd WithEvents TextBox2 As System.Windows.Forms.TextBox
FriEnd WithEvents Label1 As System.Windows.Forms.Label
FriEnd WithEvents Button2 As System.Windows.Forms.Button
FriEnd WithEvents TextBox3 As System.Windows.Forms.TextBox
 < System.Diagnostics.DebuggerStepThrough() > Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 16)
Me.TextBox1.Size = New System.Drawing.Size(320, 21)
Me.TextBox1.Text = "http://xml.sz.luohuedu.net/?ID=Url Encoding 测试"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(112, 112)
Me.Button1.Size = New System.Drawing.Size(96, 23)
Me.Button1.Text = " URL Encoding"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(72, 48)
Me.TextBox2.Size = New System.Drawing.Size(320, 21)

'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(0, 24)
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.Text = "输入URL:"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(240, 112)
Me.Button2.Size = New System.Drawing.Size(88, 23)
Me.Button2.Text = "URL Decoding"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(72, 80)
Me.TextBox3.Size = New System.Drawing.Size(320, 21)
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 149)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Text = "URL Encoding/Decoding 测试"
Me.ResumeLayout(False)

End Sub
#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = HttpUtility.UrlEncode(TextBox1.Text)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox3.Text = HttpUtility.UrlDecode(TextBox2.Text)
End Sub
End Class
Contributors: FHL