zoukankan      html  css  js  c++  java
  • 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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    GuavaCache简介(一)
    四层、七层负载均衡的区别
    腾讯云服务器 Centos6.5 安装 nginx1.12.0
    tomcat8性能优化
    JAVA 正则表达式的三种模式: 贪婪, 勉强和占有的讨论
    java中值传递和引用传递
    架构师书籍
    大型网站架构系列:20本技术书籍推荐
    RabbitMQ
    支付宝付款流程
  • 原文地址:https://www.cnblogs.com/storebook/p/12685184.html
Copyright © 2011-2022 走看看