losrob
losrob

Reputation: 115

RestSharp response returning empty

I'm using RestSharp to talk to the 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.

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.

Thanks for any help.

Upvotes: 6

Views: 5935

Answers (2)

Jason Gallagher
Jason Gallagher

Reputation: 96

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. For 4.5.2 losrob answer worked for me. 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. See this other post Default SecurityProtocol in .NET 4.5.

Upvotes: 3

losrob
losrob

Reputation: 115

I added this and it did the trick.

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

Upvotes: 3

Related Questions