ASP.NET WebService Response.Write乱码解决

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using Utility;
using System.Data;
using System.Text;

namespace Service
{
    /// <summary>
    /// api 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class api : System.Web.Services.WebService
    {

        [WebMethod]
        public void Search(string param)
        {
            string json = "[]";
            string SQL = string.Format("SELECT * FROM tbl_dnvod_video WHERE Title LIKE '%{0}%'", param);

            DataTable dt = SQLHelper.ExcuteSQL(SQL);
            if (dt != null && dt.Rows.Count > 0)
            {
                json = JsonHelper.DataTableToJson(dt);
            }

            HttpContext.Current.Response.Write(json);

            //中文出现乱码是因为使用了Encoding.UTF8等字符编码,使用Encoding.GetEncoding("GB2312")即可解决乱码问题
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            
            HttpContext.Current.Response.End();
        }
    }
}



猜你喜欢

转载自blog.csdn.net/update7/article/details/80093285
今日推荐