Underlying connection was closed (with sample code)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LS

    #1

    Underlying connection was closed (with sample code)

    Hello,
    I'm building an rss reader, works fine for all rss feeds, except for this
    one (you can try the sample code)
    The program stops on myxml.LoadXml(c ontent)

    Any ideas why this happens, I'm searching already for 2 days now.

    code
    -------------------------------------------------------------------------
    Imports System.Xml
    Imports System.Data
    Imports System.Data.Sql Client
    Imports System.Net
    Imports System.IO
    Module Module1

    Sub Main()
    Dim req As HttpWebRequest
    Dim res As HttpWebResponse
    Dim myreader As StreamReader
    Dim content As String
    req = HttpWebRequest. Create("http://www.politics.be/backend2.php")
    req.UserAgent = "Mozilla/6.0 (MSIE 6.1; Windows NT 5.1;)"
    req.MaximumAuto maticRedirectio ns = 20
    req.Timeout = 10000
    req.Proxy = GlobalProxySele ction.GetEmptyW ebProxy()
    req.KeepAlive = False
    res = req.GetResponse
    myreader = New StreamReader(re s.GetResponseSt ream)
    content = myreader.ReadTo End()
    myreader.Close( )
    myreader = Nothing
    res.Close()
    res = Nothing
    req = Nothing
    Dim myxml As New Xml.XmlDocument
    Console.WriteLi ne(content)
    myxml.LoadXml(c ontent) 'this line gives the problem
    myxml = Nothing
    Console.ReadLin e()
    End Sub

    End Module
    -------------------------------------------------------------------------------------


  • Ken Tucker [MVP]

    #2
    Re: Underlying connection was closed (with sample code)

    Hi,

    You dont need the webrequest and webresponse. Here is a simple
    example.


    Private Const filename As String = "http://www.politics.be/backend2.php"

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Dim reader As XmlTextReader = Nothing

    Try
    ' Load the reader with the data file and ignore all white space
    nodes.
    reader = New XmlTextReader(f ilename)
    reader.Whitespa ceHandling = WhitespaceHandl ing.None
    Dim bWrite As Boolean = False
    ' Parse the file and display each of the nodes.
    While reader.Read()
    Select Case reader.NodeType
    Case XmlNodeType.Ele ment
    If reader.Name = "title" Then bWrite = True
    'Trace.WriteLin e(String.Format ("<{0}>",
    reader.Name))
    Case XmlNodeType.Tex t
    If bWrite Then Trace.WriteLine (reader.Value)
    Case XmlNodeType.CDA TA
    ' Trace.WriteLine (String.Format( "<![CDATA[{0}]]>",
    reader.Value))
    Case XmlNodeType.Pro cessingInstruct ion
    'Trace.WriteLin e(String.Format ("<?{0} {1}?>",
    reader.Name, reader.Value))
    Case XmlNodeType.Com ment
    'Trace.WriteLin e(String.Format ("<!--{0}-->",
    reader.Value))
    Case XmlNodeType.Xml Declaration
    'Trace.WriteLin e("<?xml version='1.0'?> ")
    Case XmlNodeType.Doc ument
    Case XmlNodeType.Doc umentType
    'Trace.WriteLin e(String.Format ("<!DOCTYPE {0}
    [{1}]", reader.Name, reader.Value))
    Case XmlNodeType.Ent ityReference
    'Trace.WriteLin e(reader.Name)
    Case XmlNodeType.End Element
    bWrite = False
    'Trace.WriteLin e(String.Format ("</{0}>",
    reader.Name))
    End Select
    End While

    Finally
    If Not (reader Is Nothing) Then
    reader.Close()
    End If
    End Try
    End Sub


    Ken
    -----------------------
    "LS" <invalid@invali d.com> wrote in message
    news:e%23Ga2KVq FHA.1156@TK2MSF TNGP12.phx.gbl. ..
    Hello,
    I'm building an rss reader, works fine for all rss feeds, except for this
    one (you can try the sample code)
    The program stops on myxml.LoadXml(c ontent)

    Any ideas why this happens, I'm searching already for 2 days now.

    code
    -------------------------------------------------------------------------
    Imports System.Xml
    Imports System.Data
    Imports System.Data.Sql Client
    Imports System.Net
    Imports System.IO
    Module Module1

    Sub Main()
    Dim req As HttpWebRequest
    Dim res As HttpWebResponse
    Dim myreader As StreamReader
    Dim content As String
    req = HttpWebRequest. Create("http://www.politics.be/backend2.php")
    req.UserAgent = "Mozilla/6.0 (MSIE 6.1; Windows NT 5.1;)"
    req.MaximumAuto maticRedirectio ns = 20
    req.Timeout = 10000
    req.Proxy = GlobalProxySele ction.GetEmptyW ebProxy()
    req.KeepAlive = False
    res = req.GetResponse
    myreader = New StreamReader(re s.GetResponseSt ream)
    content = myreader.ReadTo End()
    myreader.Close( )
    myreader = Nothing
    res.Close()
    res = Nothing
    req = Nothing
    Dim myxml As New Xml.XmlDocument
    Console.WriteLi ne(content)
    myxml.LoadXml(c ontent) 'this line gives the problem
    myxml = Nothing
    Console.ReadLin e()
    End Sub

    End Module
    -------------------------------------------------------------------------------------



    Comment

    • LS

      #3
      Re: Underlying connection was closed (with sample code)

      > You dont need the webrequest and webresponse. Here is a simple[color=blue]
      > example.[/color]

      I know that this is possible, but why is the example not working, it works
      for all other rss feeds.
      I need the webrequest because I need info from the HEAD of the webserver.
      (the code is just an example to show what is not working.


      Comment

      • LS

        #4
        Re: Underlying connection was closed (with sample code)

        > I'm building an rss reader, works fine for all rss feeds, except for this[color=blue]
        > one (you can try the sample code)
        > The program stops on myxml.LoadXml(c ontent)
        >[/color]
        Is anybody able to reproduce this problem?


        Comment

        Working...