zoukankan      html  css  js  c++  java
  • 制作一个简易的RestClient类

    public class RestClient
        {
            private HttpWebRequest CreateHttpRequest(string url, string method)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                this.SetProxy(request);
                request.Headers.Set("Cache-Control", "No-Cache");
                request.Method = method;
                request.ContentType = "application/json";
                return request;
            }
    
            public virtual T Post<T>(string url, object requestContact) where T : class
            {
                T local;
                HttpWebRequest request = this.CreateHttpRequest(url, "Post");
                if (requestContact != null)
                {
                    using (Stream stream = request.GetRequestStream())
                    {
                        new DataContractJsonSerializer(requestContact.GetType()).WriteObject(stream, requestContact);
                    }
                }
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream2 = response.GetResponseStream())
                    {
                        local = new DataContractJsonSerializer(typeof(T)).ReadObject(stream2) as T;
                    }
                }
                return local;
            }
    
            public T Get<T>(string url, object requestContact) where T : class
            {
                T local;
                HttpWebRequest request = this.CreateHttpRequest(url, "GET");
                if (requestContact != null)
                {
                    using (Stream stream = request.GetRequestStream())
                    {
                        new DataContractJsonSerializer(requestContact.GetType()).WriteObject(stream, requestContact);
                    }
                }
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream2 = response.GetResponseStream())
                    {
                        local = new DataContractJsonSerializer(typeof(T)).ReadObject(stream2) as T;
                    }
                }
                return local;
            }
    
            private void SetProxy(HttpWebRequest request)
            {
                WebProxy proxy2 = new WebProxy
                {
                    Credentials = CredentialCache.DefaultCredentials
                };
                WebProxy proxy = proxy2;
                request.Proxy = proxy;
            }
        }
    
    一花一世界 一叶一菩提
  • 相关阅读:
    动态代理:JDK动态代理和CGLIB代理的区别
    关于国密算法 SM1,SM2,SM3,SM4 的笔记
    加密算法比较3DES AES RSA ECC MD5 SHA1等
    通过mybatis向数据库中插入日期数据
    mapreduce流程中的几个关键点
    MapReduce二次排序
    Hadoop自定义分组Group
    编译hadoop2.6.0
    ERROR [org.apache.hadoop.security.UserGroupInformation]
    Java集合分组
  • 原文地址:https://www.cnblogs.com/darjuan/p/2299604.html
Copyright © 2011-2022 走看看