• 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");

  • 相关阅读:
    代码书写过程中的一些需要培养的好习惯(持续更新)
    arm linux 移植 PHP
    arm linux 支持 wifi (wpa_supplicant)
    arm linux 移植 OpenCV
    使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整
    视频编解码 基本概念:GOP
    arm linux 移植 python3.6
    读懂反向传播算法(bp算法)
    FFmpeg命令详解
    (转)浅谈 Linux 内核无线子系统
  • 原文地址:https://www.cnblogs.com/xiabuyanyu/p/6381937.html
Copyright © 2020-2023  润新知