1

I am developing a .NET plugin that translates some data from Microsoft Dynamics Nav 2013 into Json and sends it to a webservice on a clients server.

The webservice successfully transfers the first two times i call the send method but at the third call i recieve a timeout.

Ive searched the internet and discovered that this is due to not properly closing the webresponse but i cant seem to make it work.

The following code is a lightly modified version of my send method to display my issue.

Perhaps one of you can identify the problem that causes the third consecutive call to time out?

public string SendData(string schoolName)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://" + schoolName + ".*****.dk/api/v2/stays.json");

        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = JsonConvert.SerializeObject(container);

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();

            HttpWebResponse httpResponse = null;
            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                string statusCode;
                return statusCode = httpResponse.StatusCode + " " + "Eleven blev sendt til ******";
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (httpResponse != null)
                {
                    httpResponse.Close();
                }
            }
        }
    }

Ive attemped to simplify the code as comments suggested but the issue still persists. The code now looks like this:

    public string SendData(string schoolName)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://" + schoolName + ".****.dk/api/v2/stays.json");

        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = JsonConvert.SerializeObject(container);

            streamWriter.Write(json);
        }

        using(HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
        {
            string statusCode;
            return statusCode = httpResponse.StatusCode + " " + "Eleven blev sendt til ****";
        }

    }
AronChan
  • 245
  • 3
  • 19
  • 1
    Your try/catch/finally is very complicated compared with just using a `using` statement... and you're assigning a local variable for no reason. Basically the second half of your method could be much, much simpler... are you sure you're *actually* closing the response in your real code? Usually problems like this are due to failing to dispose of a WebResponse. I note you're not disposing of the WebRequest, btw. `using` statements are your friend :) – Jon Skeet Mar 10 '15 at 09:49
  • @JonSkeet But does `WebRequest` even implement IDisposable? The [MSDN page](https://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx) doesn't mention it anywhere :s – cbr Mar 10 '15 at 09:55
  • It is complaining that it does not. It tells me type must be implicitly convertible to IDisposable. – AronChan Mar 10 '15 at 09:59
  • Ah, my mistake, sorry. I'd still use a `using` statement for the rest though - and if you close the `using` statement for the `StreamWriter` after the `Write` call, you don't need to call Flush and Close explicitly. – Jon Skeet Mar 10 '15 at 10:17
  • It turns out i had a minor error elsewhere in my project that ruined the call. The current code that i posted above with the help of you guys is correct and functioning. If you create an answer Jon Skeet i will mark it as solved. Thanks – AronChan Mar 10 '15 at 13:33

0 Answers0