C# URL帶中文引數的解決辦法
阿新 • • 發佈:2019-02-11
public static string InitChineseUrl(string chineseUrl)
{
Uri url = new Uri(chineseUrl);
System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(url.Query, System.Text.Encoding.GetEncoding("utf-8"));
string query = "";
for (int i = 0; i < nv.Count; i++)
{
if (query.Trim() == string.Empty)
{
query = "?" + nv.Keys[i] + "=" + HttpUtility.UrlEncode(nv[i], System.Text.Encoding.GetEncoding("GB2312"));
}
else
{
query += "&" + nv.Keys[i] + "=" + HttpUtility.UrlEncode(nv[i], System.Text.Encoding.GetEncoding("GB2312"));
}
}
string u = chineseUrl.Split('?')[0] + query;
return u;
}
{
Uri url = new Uri(chineseUrl);
System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(url.Query, System.Text.Encoding.GetEncoding("utf-8"));
string query = "";
for (int i = 0; i < nv.Count; i++)
{
if (query.Trim() == string.Empty)
{
query = "?" + nv.Keys[i] + "=" + HttpUtility.UrlEncode(nv[i], System.Text.Encoding.GetEncoding("GB2312"));
}
else
{
query += "&" + nv.Keys[i] + "=" + HttpUtility.UrlEncode(nv[i], System.Text.Encoding.GetEncoding("GB2312"));
}
}
string u = chineseUrl.Split('?')[0] + query;
return u;
}