【问题标题】:HttpClient vs RestSharp: how to make the same callHttpClient vs RestSharp:如何进行相同的调用
【发布时间】:2016-03-28 16:55:00
【问题描述】:

我可以使用 RestSharp 工具进行调用并收到正确的响应,但我可以使用 HTTPClient 调用做同样的事情吗?我的问题是如何格式化要在帖子中发送的值。我尝试:

var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("LocIdSbme_destination", "1386" ),
                    new KeyValuePair<string, string>("LocIdSbme_origin", "1459"),
                    new KeyValuePair<string, string>("arrival_date", "20151021 16:36:00"),
                    new KeyValuePair<string, string>("departure_date", "20151021 19:04:00"),
                    new KeyValuePair<string, string>("destination", "ABBIATEGRASSO"),
                    new KeyValuePair<string, string>("first_class_price", ""),
                    new KeyValuePair<string, string>("km_distance", "92"),
                    new KeyValuePair<string, string>("mir_destination", "S01062"),
                    new KeyValuePair<string, string>("mir_origin", "S01416"),
                    new KeyValuePair<string, string>("origin", "ABBADIA LARIANA"),
                    new KeyValuePair<string, string>("second_class_price", "4.8"),
                    new KeyValuePair<string, string>("train_ids", "[{\"train_category\": \"REG     \",\"train_id\":\"5279\"},{\"train_category\": \"REG     \",\"train_id\":\"2571\"},{\"train_category\": \"S       \",\"train_id\": \"24151\"},{\"train_category\": \"REG     \",\"train_id\": \"10539\"}]" ),
                    new KeyValuePair<string, string>("user_token",  "token"),
                    new KeyValuePair<string, string>("via_1", "1360"),
                    new KeyValuePair<string, string>("via_2", "1001")
                };

                var content = new FormUrlEncodedContent(values);


                //var x = new StringContent("", Encoding.UTF8, "application/json");


                var response = client.PostAsync("http://trenord.makeitapp.eu/nordcomstore", content).Result;

                var responseString = response.Content.ReadAsStringAsync();

但这不会产生与 RestSharp 调用相同的响应

var client2 = new RestClient("http://cccc.xxxxx.eu/mymethod");
            var request2 = new RestRequest(Method.POST);
            request2.AddHeader("postman-token", "92ee3ab6-26a0-68e7-47e2-cbf888565111");
            request2.AddHeader("cache-control", "no-cache");
            request2.AddHeader("content-type", "application/json");
            request2.AddHeader("secret", "8717fe31eb803692a1241exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            request2.AddParameter("application/json", "{    \"LocIdSbme_destination\":\"1386\",\r\n    \"LocIdSbme_origin\" : \"1459\",\r\n    \"arrival_date\" : \"20151021 16:36:00\",\r\n    \"departure_date\" : \"20151021 19:04:00\",\r\n    \"destination\" : \"ABBIATEGRASSO\",\r\n    \"first_class_price\": null,\r\n    \"km_distance\":92,\r\n    \"mir_destination\":\"S01062\",\r\n    \"mir_origin\": \"S01416\",\r\n    \"origin\":\"ABBADIA LARIANA\",\r\n    \"second_class_price\":\"4.8\",\r\n    \"train_ids\" :[\r\n       {\r\n            \"train_category\": \"REG     \",\r\n            \"train_id\":\"5279\"\r\n        },\r\n       {\r\n            \"train_category\": \"REG     \",\r\n            \"train_id\":\"2571\"\r\n        },\r\n        {\r\n            \"train_category\": \"S       \",\r\n            \"train_id\": \"24151\"\r\n        },\r\n\t\t{\r\n            \"train_category\": \"REG     \",\r\n            \"train_id\": \"10539\"\r\n        }\r\n    ],\r\n    \"user_token\":\"token\",\r\n    \"via_1\": \"1360\",\r\n    \"via_2\":\"1001\"\r\n}", ParameterType.RequestBody);
            IRestResponse response2 = client2.Execute(request2);

提前感谢大家

【问题讨论】:

    标签: httpclient restsharp


    【解决方案1】:

    问题出在你的:request2.AddParameter("application/json"

    在第一个示例中,使用 HttpClient,您使用的是 FormUrlEncoded。 尝试将 RestSharp 代码更改为您的 HttpClient:

    request2.AddParameter("application/x-www-form-urlencoded", ...)
    

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多