简体   繁体   English

RestSharp响应返回空

[英]RestSharp response returning empty

I'm using RestSharp to talk to the UPS API. 我正在使用RestSharp与UPS API通讯。

I have POSTMAN and I can talk to the API just fine however when I port it over to c# I get back an empty value for response.Content. 我有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());
            }
        }

Everything looks fine to me but I'm not sure where I have the problem at, I looked through past articles and noticed the ExecuteAsync so I tried that solution however it is still empty. 一切对我来说看起来都很好,但是我不确定问题出在哪里,我浏览了过去的文章并注意到ExecuteAsync,所以我尝试了该解决方案,但是它仍然是空的。

Thanks for any help. 谢谢你的帮助。

I added this and it did the trick. 我添加了这个,它成功了。

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

For some reason I was having this problem when my target framework was .net 4.5.2 but not when it was .net 4.7.1. 由于某种原因,当我的目标框架是.net 4.5.2时出现了这个问题,但是当它是.net 4.7.1时却没有。 For 4.5.2 losrob answer worked for me. 对于4.5.2 losrob答案为我工作。 It looks like it has to do with the default security protocol differing between the two framework versions. 看起来这与两个框架版本之间的默认安全协议有所不同。 It might a good idea to write that code like with |= like: 像||这样编写代码可能是一个好主意:

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

This will allow the default settings to the SecurityProtocol default settings to stay when future framework versions update the default. 这将允许SecurityProtocol默认设置的默认设置在以后的框架版本更新默认设置时保留。 See this other post Default SecurityProtocol in .NET 4.5 . 参见此其他文章.NET 4.5中的Default SecurityProtocol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM