• Code-Client:RestClient.cs


    ylbtech-Code-Client:RestClient.cs
    1.返回顶部
    1、
    using System;
    using System.Net;
    using System.Text;
    
    namespace Sp.Common
    {
        public enum HttpMethod
        {
            GET,
            POST,
            PUT,
            DELETE
        }
    
        public class RestClient
        {
            public string EndPoint { get; set; }    //请求的url地址  eg:   http://127.0.0.1:8080/order/order_id=1&isdel=0
            public HttpMethod Method { get; set; }    //请求的方法
            public string ContentType { get; set; } //格式类型:我用的是application/json,具体使用什么,看需求吧
            public string PostData { get; set; }    //传送的数据,当然了我使用的是json字符串
    
            public RestClient()
            {
                EndPoint = "";
                Method = HttpMethod.GET;
                ContentType = "text/xml";
                PostData = "";
            }
    
            public RestClient(string endpoint)
            {
                EndPoint = endpoint;
                Method = HttpMethod.GET;
                ContentType = "text/xml";
                PostData = "";
            }
    
            public RestClient(string endpoint, HttpMethod method)
            {
                EndPoint = endpoint;
                Method = method;
                ContentType = "text/xml";
                PostData = "";
            }
    
            public RestClient(string endpoint, HttpMethod method, string postData)
            {
                EndPoint = endpoint;
                Method = method;
                ContentType = "text/xml";
                PostData = postData;
            }
    
            public string MakeRequest()
            {
                return MakeRequest("");
            }
    
            public string MakeRequest(string parameters)
            {
                var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
    
                request.Method = Method.ToString();
                request.ContentLength = 0;
                request.ContentType = ContentType;
    
                if (!string.IsNullOrEmpty(PostData) && (Method == HttpMethod.POST || Method == HttpMethod.PUT))//如果传送的数据不为空,并且方法是post
                {
                    var encoding = new UTF8Encoding();
                    var bytes = Encoding.GetEncoding("UTF-8").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
                    request.ContentLength = bytes.Length;
    
                    using (var writeStream = request.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
    
                //if (!string.IsNullOrEmpty(PostData) && Method == HttpMethod.PUT)//如果传送的数据不为空,并且方法是put
                //{
                //    var encoding = new UTF8Encoding();
                //    var bytes = Encoding.GetEncoding("UTF-8").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
                //    request.ContentLength = bytes.Length;
    
                //    using (var writeStream = request.GetRequestStream())
                //    {
                //        writeStream.Write(bytes, 0, bytes.Length);
                //    }
                //}
    
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;
    
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }
    
                    // grab the response
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                            using (var reader = new System.IO.StreamReader(responseStream))
                            {
                                responseValue = reader.ReadToEnd();
                            }
                    }
    
                    return responseValue;
                }
            }
    
        }
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    G1(Garbage First)
    优化老年代参数的分析思路
    优化年轻代参数的分析思路
    频繁发生fullgc的原因和定位思路
    什么时候会触发老年代gc
    老年代回收器cms
    SQL的优化技巧
    常用前端性能优化
    刷新的方式
    遇到的一些坑
  • 原文地址:https://www.cnblogs.com/storebook/p/12685184.html
Copyright © 2020-2023  润新知