RestSharp 响应返回空

los*_*rob 6 c# rest

我正在使用 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());
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助。

los*_*rob 3

我添加了这个并且它成功了。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | 安全协议类型.Tls12 | 安全协议类型.Tls11 | 安全协议类型.Tls;