HttpClient与RestSharp:如何进行相同的调用

时间:2015-10-21 08:50:21

标签: httpclient restsharp

我可以使用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);

提前感谢所有人

1 个答案:

答案 0 :(得分:0)

问题在于:request2.AddParameter(&#34; application / json&#34;

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

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