您的位置:首页 > 编程语言 > ASP

ASP.NET WebService Response.Write乱码解决

2017-10-23 10:22 387 查看
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();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: