Unanswered Error in reading file from server!

  • Monday, July 16, 2012 2:21 PM
     
     

    Hey i am trying to read a file from a server and check if a string that i want is available in that file and then if true execute a function.

    This is the code that i use - 

     Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://bluezap.com/public_html/Testingread.txt"), FtpWebRequest)
                request.Method = WebRequestMethods.Ftp.DownloadFile


                request.Credentials = New NetworkCredential("", "")

                Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)

                Dim responseStream As Stream = response.GetResponseStream()
            Dim reader As New StreamReader(responseStream)

            While Not reader.EndOfStream

                If reader.ReadLine.Contains("abdfdcd") Then
                   'Execute some Function

                End If
            End While

            Console.WriteLine(reader.ReadToEnd())

            reader.Close()
            response.Close()

    Well so basically when i execute the code i get an error - 

    Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.

    And the code - While Not reader.EndOfStream is highlighted 

    What seems to be wrong here? Please help!


    Roji

All Replies

  • Monday, July 16, 2012 2:29 PM
    Moderator
     
     

    Your code should analyze the response result code.  If the stream is disposed by the time you try to read it then it has been closed, probably because the server terminated the connection.  The FtpWebResponse object instance should contain information about what happened and why.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

  • Monday, July 16, 2012 2:56 PM
     
     

    Ok can you walk me through this because i still don't understand why the connection is lost so fast?

    What is wrong with this code?


    Roji

  • Monday, July 16, 2012 3:03 PM
    Moderator
     
     

    After: Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)

    What is the value of: response.StatusCode?

    Put a breakpoint in your code a step through the execution or write the status information to the console where you can see it.

    Your code should really check the result of StatusCode and only continue if it is status "OK"; you can add code to handle other status results as appropriate.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

  • Monday, July 16, 2012 3:08 PM
     
     
    I really still dont understand o.O there seems to be nothing wrong with the code and the function i need is also executed if the string i want is there in the file but right after the code is done executing properly in the middle of executing the function that i want only i get this error? Im confused 

    Roji

  • Monday, July 16, 2012 3:17 PM
    Moderator
     
      Has Code

    You still haven't told us what the StatusCode of the response is....

    I don't know what else to tell you.... you need to check the result status code:

    Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://bluezap.com/public_html/Testingread.txt"), FtpWebRequest) request.Method = WebRequestMethods.Ftp.DownloadFile request.Credentials = New NetworkCredential("", "") Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse) If response.StatusCode = FtpStatusCode.CommandOK Then Dim responseStream As Stream = response.GetResponseStream() Dim reader As New StreamReader(responseStream) While Not reader.EndOfStream If reader.ReadLine.Contains("abdfdcd") Then 'Execute some Function End If End While Console.WriteLine(reader.ReadToEnd()) reader.Close() Else 'THE RESPONSE IS NOT OK!!! Console.WriteLine(response.StatusCode.ToString) End If response.Close()




    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"



  • Monday, July 16, 2012 3:40 PM
     
     

    ok same error again - 

    A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll

    Highlighted -  While Not reader.EndOfStream


    Roji

  • Monday, July 16, 2012 4:02 PM
     
     
    ok i figured it out FINALYYYYY!!!!

    Roji

  • Monday, July 16, 2012 4:07 PM
    Moderator
     
      Has Code

    Is the source file empty?

    Looks like the connection is closed once the end of the stream has been reached.

    Also, the return code should be 150: OpeningData, not a 200:OK, sorry about that.

    Try this:

    Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
    Console.WriteLine(response.StatusCode & ": " & response.StatusDescription)
    If response.StatusCode = FtpStatusCode.OpeningData Then
        If response.ContentLength > 0 Then
            Dim responseStream As Stream = response.GetResponseStream()
            Dim reader As New StreamReader(responseStream, System.Text.Encoding.UTF32)
            While Not reader.EndOfStream
                Dim line As String = reader.ReadLine
                If line.Contains("abdfdcd") Then
                    'Execute some Function
                End If
            End While
            reader.Close()
        Else
            Console.WriteLine("File is empty.")
        End If
    Else
        'THE RESPONSE IS NOT OpeningData!
        Console.WriteLine(response.StatusCode.ToString)
    End If
    response.Close()
     


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


  • Monday, July 16, 2012 4:14 PM
    Moderator
     
     

    Hmm... looks like content length may or may not be returned.  You might need to ask for the filesize before you attempt to download it:

    http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.contentlength


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"