“底层连接已关闭:连接意外关闭。”在某些系统上

时间:2016-02-24 11:35:36

标签: c# .net post get httpwebrequest

我有一个客户端应用程序,它通过Web服务(基于python的Web服务)与中央服务器通信。我目前正在使用post方法发送数据。这在某些系统上失败,而在其他系统上工作。 这是代码

//method with post
private bool sendDataToServicePost1 (string url,string data)
{
    try
    {
        string boundary = Guid.NewGuid().ToString().Replace("-", "");
        string newLine = "\r\n";

        // create cookie
        Cookie sessionCookie = new Cookie();
        sessionCookie.HttpOnly = true;
        sessionCookie.Domain = App.ServerIP;
        sessionCookie.Name = "session_id";
        sessionCookie.Value = App.SessionId;

        // open HTTP web request
        HttpWebRequest _webRequest = (HttpWebRequest)WebRequest.Create(url);

        // add cookie to request
        _webRequest.CookieContainer = new CookieContainer();
        _webRequest.CookieContainer.Add(sessionCookie);

        //setup POST params
        _webRequest.Method = "POST";
        _webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
        _webRequest.Timeout = 600000;

        MemoryStream ms = new MemoryStream();
        StreamWriter sw = new StreamWriter(ms);
        sw.Write(string.Format(sendFormat, boundary, newLine, "Data", data));
        sw.Write("--{0}--{1}", boundary, newLine);
        sw.Flush();
        _webRequest.ContentLength = ms.Length;

        Stream s = _webRequest.GetRequestStream();
        ms.WriteTo(s);
        ms.Flush();
        ms.Close();
        s.Close();

        var _webResponse = RadWebRetry.ReTryGetWebResponse(_webRequest);
        StreamReader _streamReader = new StreamReader(RadWebRetry.ReTryGetResponseStream(_webResponse));
        string jsonFormattedResponse = _streamReader.ReadToEnd();
        logger.Debug("The response from the save operation : " + jsonFormattedResponse);
    }
    catch (Exception excp)
    {
        logger.Fatal("Execption:{0}", excp.ToString());
        return false;
    }
}

转换post方法以使其正常工作。

//method with Get
 private bool sendDataToServiceGet((string url,string data)
{
    try
    {
        HttpWebResponse _webResponse = null;
        string urlfull =  url + "?Data=" + data;

        HttpWebRequest _webRequest = (HttpWebRequest)WebRequest.Create(urlfull);
        Cookie sessionCookie = new Cookie();
        sessionCookie.HttpOnly = true;
        sessionCookie.Domain = App.ServerIP;

        sessionCookie.Name = "session_id";
        sessionCookie.Value = App.SessionId;
        _webRequest.CookieContainer = new CookieContainer();
        _webRequest.CookieContainer.Add(sessionCookie);
        _webRequest.Method = "GET";
        _webResponse = RadWebRetry.ReTryGetWebResponse(_webRequest);
        System.IO.StreamReader _streamReader = new System.IO.StreamReader(RadWebRetry.ReTryGetResponseStream(_webResponse));
        string jsonFormattedResponse = _streamReader.ReadToEnd();
        logger.Debug("The response from the save operation : " + jsonFormattedResponse);
    }
    catch (Exception excp)
    {
        logger.Fatal("Execption:{0}", excp.ToString());
        return false;
    }
}

据我所知,这个例外不应该与系统有关,但我们在系统到系统的基础上观察它。

0 个答案:

没有答案