ASP.NET WebService Response.Write乱码解决

ASP.NET WebService Response.Write乱码解决

http://blog.csdn.net/qq285679784/article/details/78316094

原创 2017年10月23日 10:22:11
[html] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. using Utility;  
  8. using System.Data;  
  9. using System.Text;  
  10.   
  11. namespace Service  
  12. {  
  13.     /// <summary>  
  14.     /// api 的摘要说明  
  15.     /// </summary>  
  16.     [WebService(Namespace = "http://tempuri.org/")]  
  17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  18.     [System.ComponentModel.ToolboxItem(false)]  
  19.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。   
  20.     // [System.Web.Script.Services.ScriptService]  
  21.     public class api : System.Web.Services.WebService  
  22.     {  
  23.   
  24.         [WebMethod]  
  25.         public void Search(string param)  
  26.         {  
  27.             string json = "[]";  
  28.             string SQL = string.Format("SELECT * FROM tbl_dnvod_video WHERE Title LIKE '%{0}%'", param);  
  29.   
  30.             DataTable dt = SQLHelper.ExcuteSQL(SQL);  
  31.             if (dt != null && dt.Rows.Count > 0)  
  32.             {  
  33.                 json = JsonHelper.DataTableToJson(dt);  
  34.             }  
  35.   
  36.             HttpContext.Current.Response.Write(json);  
  37.   
  38.             //中文出现乱码是因为使用了Encoding.UTF8等字符编码,使用Encoding.GetEncoding("GB2312")即可解决乱码问题  
  39.             HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");  
  40.               
  41.             HttpContext.Current.Response.End();  
  42.         }  
  43.     }  
  44. }  
版权声明:本文为博主原创文章,只为分享而编,欢迎各位学习以及转载。 http://blog.csdn.net/qq285679784/article/details/78316094
 
posted @ 2018-03-12 14:40  sky20080101  阅读(127)  评论(0)    收藏  举报