我正在使用RestSharp与UPS API通讯。

我有POSTMAN,可以很好地与API通讯,但是当我将其移植到C#时,我会得到一个空值的response.Content。

private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = CustomerName;
            MessageBox.Show(username);
            MessageBox.Show(password);
            MessageBox.Show(Lic);
            try
            {
                var client = new RestClient("https://wwwcie.ups.com/rest/Track");
                var request = new RestRequest(Method.POST);
                request.AddHeader("postman-token", "73a23cf5-558a-9a83-ec80-4a224b35351a");
                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("content-type", "application/json");
                request.AddParameter("application/json", "{\r\n  \"UPSSecurity\": {\r\n    \"UsernameToken\": {\r\n      \"Username\": " + username + ",\r\n      \"Password\": " + password + "\r\n    },\r\n    \"ServiceAccessToken\": {\r\n      \"AccessLicenseNumber\": " + Lic + "\r\n    }\r\n  },\r\n  \"TrackRequest\": {\r\n    \"Request\": {\r\n      \"RequestAction\": \"Track\",\r\n      \"RequestOption\": \"activity\"\r\n    },\r\n    \"InquiryNumber\": " + textBox1.Text.Trim() + "\r\n  }\r\n}", ParameterType.RequestBody);
                string result = "";
                request.Timeout = 10000;
                //IRestResponse response = client.Execute(request);

                client.ExecuteAsync(request, (response) =>
                {
                    result = response.Content;
                   MessageBox.Show(result);
                }
                );
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }


一切对我来说看起来都很好,但是我不确定问题出在哪里,我浏览了过去的文章并注意到ExecuteAsync,所以我尝试了该解决方案,但是它仍然是空的。

谢谢你的帮助。

最佳答案

我添加了这个,它成功了。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

关于c# - RestSharp响应返回空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47584733/

10-09 19:59