Lilrich Posted February 11, 2010 Share Posted February 11, 2010 Hey everyone, I am working on a radio player for a interent radio station and want to display the contents of a text file that is stored on the internet, does anyone know how i can do this. Cheers Richard Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 You need to create a WebRequest instance to make the call to the remote server to get the text file: ' The url of the file. Dim url as String = "http://www.mydomain.com/file.txt" ' Create our web request, and get its response. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = request.GetResponse() ' Read the content from the stream. Dim reader As StreamReader = new StreamReader(response.GetResponseStream()) Dim content As String = reader.ReadToEnd() ' Dispose of our managed types. reader.Dispose() response.Dispose() request.Dispose() null_ 1 Share Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 I don't mean to be really really stupid but copying and pasting this to my project wont work will it? I can copy my project code here if that helps? Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Wrap it up inside a function: Import System.IO Import System.Web Public Function GetRemoteFile(ByVal url As String) As String ' Create our web request, and get its response. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = request.GetResponse() ' Read the content from the stream. Dim reader As StreamReader = new StreamReader(response.GetResponseStream()) Dim content As String = reader.ReadToEnd() ' Dispose of our managed types. reader.Dispose() response.Dispose() request.Dispose() Return content End Function And then you can call it as such: Dim remoteFileContent As String = GetRemoteFile("http://www.mydomain.com/test.txt") Just add it in wherever you feel is appropriate. null_ 1 Share Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 I keep getting the error url is already delacred as part of this method. Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Can you post your code? Not the entire project, just the function, and how you are calling it. Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 Imports Un4seen.Bass Imports System.IO Imports System.Web Imports System.Net Public Function GetRemoteFile(ByVal url As String) As String ' The url of the file. Dim url As String = "http://dl.dropbox.com/u/1038051/test.txt" ' Create our web request, and get its response. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = request.GetResponse() ' Read the content from the stream. Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) Dim content As String = reader.ReadToEnd() ' Dispose of our managed types. 'reader.Dispose() 'response.Dispose() 'request.Dispose() Return content End Function Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt") End Sub End Class Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Ah, in my refactored function, I removed the url declaration as its declared earlier as a parameter: Public Function GetRemoteFile(ByVal url As String) As String ' Create our web request, and get its response. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = request.GetResponse() ' Read the content from the stream. Dim reader As StreamReader = new StreamReader(response.GetResponseStream()) Dim content As String = reader.ReadToEnd() ' Dispose of our managed types. reader.Dispose() response.Dispose() request.Dispose() Return content End Function Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 I am still stuck....i am getting frustrated now. Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Ah, my bad, HttpWebRequest and HttpWebResponse Dispose methods are protected. You can remove them from your code, so remove the lines: request.Dispose() response.Dispose() Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 Yes, the app runs now but if i call the function inside a label function the text dosent display that is inside the .txt file and i know their is some in their http://dl.dropbox.com/u/1038051/test.txt Public Function GetRemoteFile(ByVal url As String) As String ' Create our web request, and get its response. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = request.GetResponse() ' Read the content from the stream. Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) Dim content As String = reader.ReadToEnd() ' Dispose of our managed types. reader.Dispose() Return content End Function Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt") End Sub End Class Link to comment Share on other sites More sharing options...
0 Rob Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 This: Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt") End Sub Doesn't do anything useful. It just assigns the contents of the file to a variable. There's no logic in there to display it in any fashion. Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 This: Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click Dim remoteFileContent As String = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt") End Sub Doesn't do anything useful. It just assigns the contents of the file to a variable. There's no logic in there to display it in any fashion. Do you have any code that may help me display it? :rolleyes: Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 How do you want it displayed? Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 Is it possible to put it in a text box or something like that and have it scroll along? Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Lol, add a textbox to your form, and then set the content: TextBox1.Text = GetRemoteFile("http://dl.dropbox.com/u/1038051/test.txt") We can only show you the door, its up to you to explore what's on the other side. Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 Roger that - i am highly greatful for all of the help you have given me...really i am. Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 No probs, let us know if you have any other questions. Link to comment Share on other sites More sharing options...
0 Lilrich Posted February 11, 2010 Author Share Posted February 11, 2010 YAY!!! Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 11, 2010 Veteran Share Posted February 11, 2010 Cool, glad we could help, feel free to give reputation ;) Lilrich 1 Share Link to comment Share on other sites More sharing options...
Question
Lilrich
Hey everyone,
I am working on a radio player for a interent radio station and want to display the contents of a text file that is stored on the internet, does anyone know how i can do this.
Cheers
Richard
Link to comment
Share on other sites
19 answers to this question
Recommended Posts