Exception: "The request was aborted: the connection was unexpectedly closed." When you call two methods, the same object

I had a problem that many people experienced, but all the solutions available on the Internet are specific to their scenarios. I tried the suggestions offered, but still no luck I tried:

1.) req.KeepAlive = false;
2.) req.ProtocolVersion = HttpVersion.Version10; (this solved a different exception that I was getting)

I have two methods that pass the XML string to the server, and get the responses back in two ways:

public string userDeviceQuery(string userID)
    {
        string query = "xml=<query>";
        query += "<appInfo>";
        query += "<appID>" + appID + "</appID>";
        query += "<appCertificate>" + appCertificate + "</appCertificate>";
        query += "</appInfo>";
        query += "<userDevicesQuery>";
        query += "<userID>";
        query += userID;
        query += "</userID>";
        query += "</userDevicesQuery>";
        query += "</query>";

        using (Stream str = req.GetRequestStream())
        {
            str.Write(System.Text.Encoding.ASCII.GetBytes(query), 0, query.Length);
        }

        WebResponse res = req.GetResponse();

        string stringResponse;
        using (StreamReader reader = new StreamReader(res.GetResponseStream()))
        {
            stringResponse = reader.ReadToEnd();
        }

        string result = parseDeviceQueryRes(stringResponse);
        return result;
    }

and

public void logoutOfEM(string deviceName)
    {
        string lgRequest = "xml=<request>";
               lgRequest += "<appInfo>";
               lgRequest += "<appID>" + appID + "</appID>";
               lgRequest += "<appCertificate>" + appCertificate + "</appCertificate>";
               lgRequest += "</appInfo>";
               lgRequest += "<logout>";
               lgRequest += "<deviceName>";
               lgRequest += deviceName;
               lgRequest += "</deviceName>";
               lgRequest += "</logout>";
               lgRequest += "</request>";

         using (Stream str = req.GetRequestStream())
        {
            str.Write(System.Text.Encoding.ASCII.GetBytes(lgRequest), 0, lgRequest.Length);
        }

        WebResponse res = req.GetResponse();

        using (StreamReader reader = new StreamReader(res.GetResponseStream()))
        {
            stringResponse = reader.ReadToEnd();
        }
        string stringResponse = reader.ReadToEnd();
    }

They are part of the EMAPI class. I can use both methods just fine, but if I try to use them in reverse order with the same object representing EMAPI, I get the exception described above. I'm not sure what causes the connection to close, I would like to be able to keep it open and close it with the de-constructor, if possible.

, , Cisco Extension Mobility API, , , .

.

+1
2

HttpWebRequest . HttpWebRequest . : HttpWebRequest ?. .NET , , , req.KeepAlive = true.

, / WebResponse .

+2

'using' IDisposables. -, , StreamReaders.

Edit

.

:

Stream str = req.GetRequestStream();
str.Write(System.Text.Encoding.ASCII.GetBytes(lgRequest), 0, lgRequest.Length);
str.Close();

:

using(Stream str = req.GetRequestStream())
{
    str.Write(System.Text.Encoding.ASCII.GetBytes(lgRequest), 0, lgRequest.Length);
}

using IDisplosable .

- , . , . , 'using'.

StreamReader reader = new StreamReader(res.GetResponseStream());
reader.Close(); //Calling CLOSE before trying to use the reader
string stringResponse = reader.ReadToEnd();

public void logoutOfEM(string deviceName)
    {
        string lgRequest = "xml=<request>";
               lgRequest += "<appInfo>";
               lgRequest += "<appID>" + appID + "</appID>";
               lgRequest += "<appCertificate>" + appCertificate + "</appCertificate>";
               lgRequest += "</appInfo>";
               lgRequest += "<logout>";
               lgRequest += "<deviceName>";
               lgRequest += deviceName;
               lgRequest += "</deviceName>";
               lgRequest += "</logout>";
               lgRequest += "</request>";
     using (Stream str = req.GetRequestStream())
    {
        str.Write(System.Text.Encoding.ASCII.GetBytes(lgRequest), 0, lgRequest.Length);
    }

    WebResponse res = req.GetResponse();

    var response = string.Empty;
    using(StreamReader reader = new StreamReader(res.GetResponseStream()))
    {
        response = reader.ReadToEnd();
    }


}
+1

Source: https://habr.com/ru/post/1525024/


All Articles