美文网首页
2018-04-19 get 请求

2018-04-19 get 请求

作者: 863536 | 来源:发表于2018-04-19 10:47 被阅读0次

join suburb

public static string get(string url,int timeout=30000){
     var request=(HttpWebRequest)WebRequest.Create(url);
     request.TimeOut=timeout;
     request.Method="GET";
     request.ConnectType="text/html;charset=UTF-8";
     
     var response=(HttpWebResponse)request.GetResponse(); 
      using(var myResponseStream=response.GetResponseStream())  {
              if(myResponseStream==null) return string.Empty;
              using(var myStreamReader=new   StreamReader(myResponseStream,Encoding.UTF8)){
               return myResponseStream.ReadToEnd();
         }
       }
}

possible

public class ActionToken{
      [JsonProperty(PropertyName="access_token")]
      public string Accesstoken{get;set;}
      [JsonProperty(PropertyName="expires_in")]
      public string Expiresin{get;set;}
}
  public static AccessToken GetToken(string AppID, string AppSecret, string grant_type = "client_credential")
        {
            var url = string.Format(Weixi.getAccessToken, grant_type, AppID, AppSecret);
            return Get<AccessToken>(url);

        }
  public static T Get<T>(string url)
        {
            var returnJson = Get(url);
            CheckThrowError(returnJson);
            return GetJson<T>(returnJson);
        }
        /// <summary>
        /// Json字符串转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T GetJson<T>(string jsonString)
        {
            return JsonConvert.DeserializeObject<T>(jsonString);
        }

   public static void CheckThrowError(string returnJson)
        {
            if (returnJson.Contains("errcode"))
            {
                //可能发生错误
                var errorResult = GetJson<ResError>(returnJson);
                if (errorResult.errcode != ResCode.请求成功)
                {
                    throw new Exception(string.Format("微信请求发生错误!错误代码:{0},说明:{1}", (int)errorResult.errcode, errorResult.errmsg));
                }
            }
        }

补充 实际开发过程中遇到不同的问题

  1. 获取的数据乱码 原因是请求数据 gzip 压缩 需要修改代码
   using (var response = (HttpWebResponse)request.GetResponse())
            {
                var myResponseStream = response.GetResponseStream();
                if (response.ContentEncoding.ToLower() == "gzip")
                {
                    myResponseStream = new GZipStream(myResponseStream, CompressionMode.Decompress);
                }
                if (myResponseStream == null)
                    return string.Empty;
                using (var myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8))
                {
                    return myStreamReader.ReadToEnd();
                }
            }

2.获取某些数据需要授权 需要加上这句

  var request = (HttpWebRequest)WebRequest.Create(url);

  request.Headers.Add("Authorization", token);

相关文章

  • 2018-04-19 get 请求

    join suburb possible 补充 实际开发过程中遇到不同的问题 获取的数据乱码 原因是请求数据 gz...

  • Okhttp3

    简介 配置 请求思路 get请求思路 post请求思路 get,post 同步和异步请求 异步请求(get) 同步...

  • Retrofit2基本使用

    导入 Get请求 1.普通get请求 定义get请求接口 通过retrofit完成请求 addConverterF...

  • SpringBoot开发接口

    1、模拟get请求2、模拟get请求返回cookie3、模拟get请求携带cookie信息4、模拟get请求携带参...

  • iOS请求方法和网络安全

    GET和POST请求 GET和POST请求简介 GET请求模拟登陆 POST请求模拟登陆 GET和POST的对比 ...

  • java发送http请求

    restTemplate get请求 post请求 apache.http.client get请求 post请求...

  • Okhttp的基础使用

    1.添加依赖: 2.配置请求: 2.1 GET请求: GET 同步请求: GET异步请求: 2.2 POST请...

  • GET和POST的区别

    GET: GET 请求可被缓存GET 请求保留在浏览器历史记录中GET 请求可被收藏为书签GET 请求不应在处理敏...

  • get和post请求区别

    get请求和post请求 差别 get请求回退时无反应,post请求回退时会再次发起请求。 GET请求只能进行ur...

  • iOS请求方法和网络安全

    GET和POST请求GET和POST请求简介GET请求模拟登陆POST请求模拟登陆GET和POST的对比保存用户信...

网友评论

      本文标题:2018-04-19 get 请求

      本文链接:https://www.haomeiwen.com/subject/zjgikftx.html