how to solve "An existing connection was forcibly closed by the remote host" query?

275 Views Asked by At

I am downloading image by using asp.net c# then my code goes to catch block and show me error like this

An existing connection was forcibly closed by the remote host.

when i click my download button it takes me in this and i directly goes in catch section and i got this error in response = (HttpWebResponse)request.GetResponse(); function

protected void ImageDownloadClick_OnClick(object sender, EventArgs e)

    {

    string stoneid = HdnStoneID.Value;
        MemoryStream MemoryStream = new MemoryStream();
        int readBytes = 0;
        byte[] buffer = new byte[4096];
        string tempFileName = "ABCD-" + 025-A-11-1+ ".jpg";
        HttpWebRequest request = null;
        HttpWebResponse response = null;

        //request = (HttpWebRequest)HttpWebRequest.Create("https://15R-75490.jpg");
        request = (HttpWebRequest)HttpWebRequest.Create("https://025-A-11-1/still.jpg");
        request.Timeout = 8000;  //8000 Not work 
        response = (HttpWebResponse)request.GetResponse();
        try
            {
               
                Stream responseStream = response.GetResponseStream();


                do
                {
                    readBytes = responseStream.Read(buffer, 0, buffer.Length);
                    MemoryStream.Write(buffer, 0, readBytes);
                } while (readBytes > 0);
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=" + tempFileName);
                Response.ContentType = "image/jpeg";


                Response.BinaryWrite(MemoryStream.GetBuffer());
                Response.Flush();
            }
            catch (Exception Ex)
            {
            Stream responseStream = response.GetResponseStream();


            do
            {
                readBytes = responseStream.Read(buffer, 0, buffer.Length);
                MemoryStream.Write(buffer, 0, readBytes);
            } while (readBytes > 0);
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "imageNotAvail", "javascript:imageNotAvail();", true);
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + tempFileName);
            Response.ContentType = "image/jpeg";


            Response.BinaryWrite(MemoryStream.GetBuffer());
            Response.Flush();
        }
    }
0

There are 0 best solutions below