TOP

Restsharp常見格式的發送分析(一)
2017-10-13 10:43:29 】 瀏覽:12093
Tags:

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);
                       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流的方式傳輸的這時候使用上面添加參數的方式就無效了,如果發送呢优乐棋牌app下载,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  
		

請關注公眾號獲取更多資料



首頁 上一頁 1 2 3 下一頁 尾頁 1/3/3
】【打印繁體】【】【】 【】【】【】 【關閉】 【返回頂部
上一篇關于Linq對DataTable和List各自的.. 下一篇面向對象思想----不看后悔!