zoukankan      html  css  js  c++  java
  • webservice 写的加载url并返回json

       写了一个接口实现调用别人的接口返回json数据,在自己的接口里输出json字符串。下面有关于json字符串返回值的中文乱码问题。

      

      

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Services;
    
    namespace search
    {
        /// <summary>
        /// SearchWebService 的摘要说明
        /// </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 SearchWebService : System.Web.Services.WebService
        {
    
            [WebMethod]
            public void LbsSearch(string a,string b)
            {
                string url = string.Format("http://url地址?a={0}&b={1}", a, b);
                var json=GetHtml(url);
                //TotalInfo TotalInfo = JsonConvert.DeserializeObject<TotalInfo>(json);
                Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Context.Response.Write(json);
                Context.Response.End();
            }
            private string GetHtml(string URL)
            {           
                var request = (HttpWebRequest)WebRequest.Create(URL);
               var response = request.GetResponse();
               using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
               {
                 return reader.ReadToEnd();
               } } } }

      调用的接口读取的编码是utf-8编码,直接response.write(json字符串) 在google被浏览器自动解码了不会出现中文乱码,而在火狐和ie上则会出现中文乱码,只需要在返回之前给他中文编码就行了Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

  • 相关阅读:
    python mysql Connect Pool mysql连接池 (201
    mysql: see all open connections to a given database?
    mysql int(3)与int(11)的区别
    Configure Amazon RDS mysql to store Chinese Characters
    Maximum length of a table name in MySQL
    JDBC Tutorials: Commit or Rollback transaction in finally block
    Java transient关键字使用小记
    Java构造和解析Json数据的两种方法详解二
    精选30道Java笔试题解答
    Visual Studio最好用的快捷键(你最喜欢哪个)
  • 原文地址:https://www.cnblogs.com/xiabuyanyu/p/6381937.html
Copyright © 2011-2022 走看看