TOP

Restsharp常见格式的发送分析(一)
2017-10-13 10:43:29 】 浏览:11298
Tags:Restsharp 常见 格式 发送 分析

1.传递匿名对象JSON格式

 public string Pay(string apisecret, string apikey, string token)
        {
            try
            {
                string url = "https://******//test";            
                var client = new RestClient(url);             
          var request = new RestRequest(Method.POST);
                var tran = new
                {
                    merchant_ref = "Astonishing-Sale",
                    transaction_type = "purchase"
                };
                //生成随机数
                RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
                byte[] random = new Byte[8];
                rng.GetBytes(random);
                string NONCE = Math.Abs(BitConverter.ToInt64(random, 0)).ToString();
                //生成时间戳
                string TIMESTAMP = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds).ToString();
                //获取JSON字符内容
                string paybody = string.Empty;

                if (tran.transaction_type != null)
                {
                    paybody = SimpleJson.SerializeObject(tran);
                }
  
                string auth = GetAuth(apisecret, apikey, token, paybody, NONCE, TIMESTAMP);
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("apikey", apikey);
                request.AddHeader("token", token);
                request.AddHeader("Authorization", auth);
                request.AddHeader("nonce", NONCE);
                request.AddHeader("timestamp", TIMESTAMP);
                request.AddJsonBody(tran);//自动转换为JSON
                IRestResponse response = client.Execute(request);
                var content = response.Content;               return content;
            }
            catch (Exception e)
            {return string.Empty;

            }
        }

 

  

 

2.multipart/form-data上传文件

根据Restsharp源码可以发现如果使用了AddFile了会自动添加multipart/form-data参数           

            string server = "http://*****//Upload";
            string picurl = "C:\\Users\\Administrator\\Desktop\\555.png";
            string testparams = "testpicupload";
            RestClient restClient = new RestClient(server);
            //restClient.Encoding = Encoding.GetEncoding("GBK");//设置编码格式
            RestRequest restRequest = new RestRequest("/images");
            restRequest.RequestFormat = DataFormat.Json;
            restRequest.Method = Method.POST;
            //restRequest.AddHeader("Authorization", "Authorization");//当需要设置认证Token等情况时使用默认不需要
            //restRequest.AddHeader("Content-Type", "multipart/form-data");//如果使用了AddFile会自动添加否则请手动设置
            restRequest.AddParameter("test", testparams);
            // restRequest.AddFile("pic", picurl);//压缩格式
            restRequest.AddFile("pic", picurl, contentType: "application/x-img");    //非压缩格式 如果此处不设置为contentType 则默认压缩格式
            var response = restClient.Execute(restRequest);
            var info= response.Content;

 

 

3.直接传递不带参数的RequestBody:有的时候接口并不包含参数名而是直接以body流的方式传输的这时候使用上面添加参数的方式就无效了,如果发送呢,RestSharp里保留了AddBody和 ParameterType.RequestBody来实现

request.AddParameter("text/xml", body, ParameterType.RequestBody);
req.AddParameter("application/json", body, ParameterType.RequestBody);

  

根据AddBody源码发现实际转换最终使用的都是 AddParameter(contentType, serialized, ParameterType.RequestBody) 这个方法

 public IRestRequest AddBody(object obj, string xmlNamespace)
        {
            string serialized;
            string contentType;
            switch (this.RequestFormat)
            {
                case DataFormat.Json:
                    serialized = this.JsonSerializer.Serialize(obj);
                    con  
		
Restsharp常见格式的发送分析(一) https://www.cppentry.com/bencandy.php?fid=106&id=163897

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于Linq对DataTable和List各自的.. 下一篇面向对象思想----不看后悔!