c# 关于url编码的问题???

来源:百度知道 编辑:UC知道 时间:2024/05/27 13:28:01
我在winfrom中写了一个对url编码的类,如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;

namespace 从google中搜索邮箱地址
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string s = "%E4%BA%BA%E6%B0%91%E5%8C%BB%E9%99%A2";
string m = HttpContext.Current.Server.HtmlDecode(s);

}
}
}
但是它报错了:未将对象引用设置到对象的实例。

HttpContext.Current 是表示当前 http 请求处理线程,但你这个是 windows 程序,很显然没有 http 请求处理,所以你应该使用其它的方法实现

string s = "%E4%BA%BA%E6%B0%91%E5%8C%BB%E9%99%A2";
string m = System.Web.HttpUtility.HtmlDecode(s);