StreamReader.ReadtoEnd and request.GetResponse get "Attempted to read or write protected memory" error


hi,i write program web page content.when runs few minutes ,it crash "attempted read or write protected memory" error.i write method in class , create threads use web content.

here code:

 public void httpwebcontent(string url)
        {
            try
            {
                httpwebrequest request = (httpwebrequest)webrequest.create(new system.uri(url));
                request.useragent = "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; win64; x64; trident/5.0)";
                request.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;q=0.5";
                request.headers.add("accept-encoding", "gzip,deflate");
                request.protocolversion = httpversion.version10;
                request.keepalive = false;
               

request.method = "get";

                httpwebresponse response = null;
                try
                {
                    response = (httpwebresponse)request.getresponse();//////////////////here
                    if (response == null || response.statuscode == httpstatuscode.requesttimeout)
                    {
                        _haswebpage = false;
                        response.close();
                        response.dispose();
                        return;
                    }

                    try
                    {                   
                        string responsecharset = response.contentencoding;
                        stream webstream = response.getresponsestream();

                        if (response.contentencoding.tolower().contains("gzip"))
                            webstream = new gzipstream(webstream, compressionmode.decompress);
                        else if (response.contentencoding.tolower().contains("deflate"))
                            webstream = new deflatestream(webstream, compressionmode.decompress);

                        string charset = response.characterset;
                        charset = regex.replace(charset, @"[ ""']", "");
                        if (charset == "iso-8859-1" || string.isnullorempty(charset))
                            charset = "utf-8";

                        if (webstream.canread == true && webstream != null)
                        {
                            using (streamreader strreader = new streamreader(webstream, encoding.getencoding(charset)))
                            {
                                _requesturl = response.responseuri.tostring();
                                _content = strreader.readtoend();   /////////////////////////here
                            }
                        }

                        webstream.close();
                        webstream.dispose();

                    }
                    catch (exception ex)
                    {
                        if (ex.tostring().indexof("time out") > -1 || ex.tostring().indexof("transport connection") > -1)
                        {
                            _haswebpage = false;
                        }
                    }
                    finally
                    {
                        response.close();
                        response.dispose();
                    }             

                    if (!string.isnullorempty(_content))
                        _haswebpage = true;
                    else
                        _haswebpage = false;
                }
                catch (webexception ex)
                {
                    response = (httpwebresponse)ex.response;
                   .....
                    response.close();
                    response.dispose();
                    return;
                }
            }
            catch
            {
                _haswebpage = false;
                return;
            }
        }



hi,i write program web page content.when runs few minutes ,it crash "attempted read or write protected memory" error.i write method in class , create threads use web content.

that exception means 1 of 2 things:

- run unmanaged code , of course messed poitner arythmetic (inlcuding array bounds).

- there serious binary fault in ram, disk or @ least instalaltion of .net framework.

a third rare case can unlreated process might interefere. had issues avg 2015 disrupting working of winforms.dll. 1 of other two.

also please put code in code blocks:

 public void httpwebcontent(string url)         {             try             {                 httpwebrequest request = (httpwebrequest)webrequest.create(new system.uri(url));                 request.useragent = "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; win64; x64; trident/5.0)";                 request.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;q=0.5";                 request.headers.add("accept-encoding", "gzip,deflate");                 request.protocolversion = httpversion.version10;                 request.keepalive = false;                  request.method = "get";                  httpwebresponse response = null;                 try                 {                     response = (httpwebresponse)request.getresponse();//////////////////here                     if (response == null || response.statuscode == httpstatuscode.requesttimeout)                     {                         _haswebpage = false;                         response.close();                         response.dispose();                         return;                     }                      try                     {                                            string responsecharset = response.contentencoding;                         stream webstream = response.getresponsestream();                          if (response.contentencoding.tolower().contains("gzip"))                             webstream = new gzipstream(webstream, compressionmode.decompress);                         else if (response.contentencoding.tolower().contains("deflate"))                             webstream = new deflatestream(webstream, compressionmode.decompress);                          string charset = response.characterset;                         charset = regex.replace(charset, @"[ ""']", "");                         if (charset == "iso-8859-1" || string.isnullorempty(charset))                             charset = "utf-8";                          if (webstream.canread == true && webstream != null)                         {                             using (streamreader strreader = new streamreader(webstream, encoding.getencoding(charset)))                             {                                 _requesturl = response.responseuri.tostring();                                 _content = strreader.readtoend();   /////////////////////////here                             }                         }                          webstream.close();                         webstream.dispose();                      }                     catch (exception ex)                     {                         if (ex.tostring().indexof("time out") > -1 || ex.tostring().indexof("transport connection") > -1)                         {                             _haswebpage = false;                         }                     }                                         {                         response.close();                         response.dispose();                     }                                   if (!string.isnullorempty(_content))                         _haswebpage = true;                     else                         _haswebpage = false;                 }                 catch (webexception ex)                 {                     response = (httpwebresponse)ex.response;                    .....                     response.close();                     response.dispose();                     return;                 }             }             catch             {                 _haswebpage = false;                 return;             }         }

after short noticed, error handling - plain insane. sorry, no other way describe this:

catch (exception ex) {     if (ex.tostring().indexof("time out") > -1 || ex.tostring().indexof("transport connection") > -1)     {         _haswebpage = false;     } }

you catch exception.
string matching find out wich exception got rather type matching.
, don't throw on or @ least expose/log if not 1 expected.

i take bet have bajillion swallowed fatal exceptions based on code. code peice alone account problem ever encoutered programm.

read on proper error handling. favor , follow guidelines:

http://www.codeproject.com/articles/9538/exception-handling-best-practices-in-net

vexing exceptions - fabulous adventures in coding - site home - msdn blogs



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

wcf-sql adapter bug for operation on same table

Azure DocumentDB Owner resource does not exist