none
How to properly close a JSON Post using RestSharp? RRS feed

  • Question

  • Hi All,

    I'm trying to write a json POST message using .NET 4.5, with VS 2015.

    The following code is able to post the message successfully, but for some odd reason, about (1) minute after the code executes, and closes. The server that received the message, will get a crash alert, and shutdown the message service.

    I tried placing a sleep statement at the end of my code for 2 minutes, and that does give the server time to abort the connection, without taking down the service. I also checked the ServicePointManager, RestClient, and IResponse classes, and I dont see a Close, or Dispose method I can use to close the connection on my end. Any idea how I can close my application  successfully without taken down the server services? 

    using RestSharp;
    using RestSharp.Authenticators;
    using Newtonsoft.Json;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var client = new RestClient("https://testdev.com:5109/Test");

            var request = new RestRequest();
            request.Method = Method.POST;
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            request.RequestFormat = DataFormat.Json;
    IRestResponse response = client.Execute(request);



    Thanks!

    Tuesday, September 24, 2019 11:21 PM

Answers

  • Hi mlong219,

    Thank you for posting here.

    Based on my test, your question may be related to the website you provided. Because I tested it with my website, I could not reproduce your problem.

    Here is my test code.

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var client = new RestClient("https://www.google.com/");
    
                var request = new RestRequest();
                request.Method = Method.POST;
                request.AddParameter("application/json", "test", ParameterType.RequestBody);
                request.RequestFormat = DataFormat.Json;
                IRestResponse response = client.Execute(request);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mlong219 Wednesday, October 2, 2019 10:46 PM
    Wednesday, September 25, 2019 2:48 AM
    Moderator

All replies

  • This seems to be a problem on the server side.

    No matter how deformed the request is, it should not be able to crash the server.

    Btw, is there any error message on the Event Viewer of the server? By default unhandled exceptions will be written there.

    Wednesday, September 25, 2019 1:19 AM
    Answerer
  • Hi mlong219,

    Thank you for posting here.

    Based on my test, your question may be related to the website you provided. Because I tested it with my website, I could not reproduce your problem.

    Here is my test code.

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var client = new RestClient("https://www.google.com/");
    
                var request = new RestRequest();
                request.Method = Method.POST;
                request.AddParameter("application/json", "test", ParameterType.RequestBody);
                request.RequestFormat = DataFormat.Json;
                IRestResponse response = client.Execute(request);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mlong219 Wednesday, October 2, 2019 10:46 PM
    Wednesday, September 25, 2019 2:48 AM
    Moderator
  • Hi mlong219,

    Thank you for posting here.

    Based on my test, your question may be related to the website you provided. Because I tested it with my website, I could not reproduce your problem.

    Here is my test code.

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var client = new RestClient("https://www.google.com/");
    
                var request = new RestRequest();
                request.Method = Method.POST;
                request.AddParameter("application/json", "test", ParameterType.RequestBody);
                request.RequestFormat = DataFormat.Json;
                IRestResponse response = client.Execute(request);

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    It turned out to be an issue with the server. The team has found one solution and still investigating.

    Thanks for the help!

    Wednesday, October 2, 2019 10:48 PM