create txtfile from website and save to server - vb.net

I have attempted to find a way to download a file (from a PC running a Winform written in VB) from a web directory but could not get this done due to the date stamp that gets generated once the file is saved (filename must be precise). So now I'm trying the reverse (saving the file to the PC directly from the creation of the file).
Does anyone have any suggestions on methods to use (please no FTP due to proxy restrictions, same goes for client/server TCP/UDP). this will always be a .txt file.
thanks in advance.

If you can see the file contents by navigating to the URL, you can use an HttpWebRequest and a StreamReader / StreamWriter to save the data.
I'm downloading an example from http://textfiles.com/100/914bbs.txt
Dim request As HttpWebRequest
request = WebRequest.Create("http://textfiles.com/100/914bbs.txt")
request.Method = "GET"
Dim response = request.GetResponse
Using reader As New StreamReader(response.GetResponseStream)
Using writer As New StreamWriter("C:\myfilename.txt")
writer.Write(reader.ReadToEnd)
End Using
End Using

Related

using VB.net how do I pass credentials to ssrs report server?

I'm trying to pass credentials to the ssrs server to load the report as PDF but am getting 401 unauthorized error.
I have a vb.net web forms project that I'm working on and it is trying to load a report on a remote SQL Server 2008 r2 machine. I'm trying to load the report and have it download as a pdf. My code is supposed to load the file as a data stream which allows the user to save the file. Pointing the browser directly to the url of the report, I can get the PDF, but need to supply credentials. I'm trying to avoid the need to login to get the report. The code I'm using is supposed to pass credentials but instead of returning the report, I get 401 unauthorized error.
Dim ReportUrl As String = "http://server/ReportServer/Pages/ReportViewer.aspx?%2fReport%2fParishreport&rs:Command=Render&parameter=" & session("parameter") & "&rs:format=pdf"
Dim ReportWebResponse As HttpWebResponse
Dim Request As HttpWebRequest = CType(WebRequest.Create(ReportUrl), HttpWebRequest)
'Request.Credentials = CredentialCache.DefaultCredentials
Request.Credentials = New NetworkCredential("user", "password")
ReportWebResponse = CType(Request.GetResponse(), HttpWebResponse)
Dim ReportResponseStream As StreamReader = New StreamReader(ReportWebResponse.GetResponseStream(), New UnicodeEncoding)
Dim objMemoryStream As New MemoryStream(New UnicodeEncoding().GetBytes(ReportResponseStream.ReadToEnd()))
Response.Clear()
Response.AddHeader("Accept-Header", objMemoryStream.Length.ToString())
Response.ContentType = "application/pdf"
Response.OutputStream.Write(objMemoryStream.ToArray(), 0, Convert.ToInt32(objMemoryStream.Length))
ReportResponseStream.Close()
Response.Flush()
Try
Response.End()
Catch
End Try
I expected the browser to allow me to save the PDF or to display the pdf in the browser(Firefox built in PDF viewer). Using specified username and password, I get 401 unauthorized error. Using defaultcredentials, I get a blank PDF of the correct number of pages.
I have added the domain account to the report server: 1. using SQL server management studio 2. using report server web interface adding folder and report permissions.
What is the preferred method for supplying credentials to the reporting server such that a single domain account can be used to control users ability to view the report?

VB count files via ftp

how to make VB count the number of items available in a folder of my website? i need to use FTP? i can make VB upload files, download files, or whatever,all using FTP, but i need him show me how many items i have in the specific folder.
I use this code for connect to FTP:
Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("adress"), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("user", "pass")
You can list the files in the directory using the method ListDirectoryDetails:
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

How to read a CSV file from a remote server in VB.NET?

I am trying to read a CSV file from a remote server and I am not sure what is the best way to do it in VB.NET. The csv file is about 600Kbytes.
Should I download the file locally and then read it line by line?
I tried the following code. It can output a small text file, but not my csv file.
Dim WebClient As New WebClient()
WebClient.Proxy = Nothing
Dim remotetext As String = WebClient.DownloadString("http://localhost/test.csv")

Can't open IIS w3c log vb.net

I have been writing an app in vb.net to copy the last few lines of an IIS W3C log file every few minusts to A file that will be used for some remote reporting.
Everything works when I test on my local PC but when I try it on the live IIS server it tells me the file is locked by another process, I take it IIS has it locked...
It does read in some of the sites log files (more than on site on the server) but not others, tells me locked/in use.
Why is it that I can always open the file in notepad but not open it in my app?
The Code:
Dim linex = ""
Dim Line = ""
'### IT ERROS OUT ON THE NEXT LINE ###
Using sr As New StreamReader("C:\inetpub\logs\LogFiles\W3SVC14\u_ex130702.log")
Do Until sr.EndOfStream
linex = sr.ReadLine()
line = line & linex & vbCrLf
Loop
End Using
You need to specify correct sharing options and open mode when creating underlying FileStream. Since there is no constructor of StreamReader that passes all necessary arguments you need to construct FileStream first using FileStream(String, FileMode, FileAccess, FileShare) and than create StreamReader on it using SrteamReader(Stream).
I think following should open IIS log file while it is being written to by IIS (if not - try other combinations of flags)
Using stream As New New FileStream( _
"Test####.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using sr As New StreamReader(stream)

how to read the file which is in application itself and how to add in setup?

I have a class library which reads the XML file.
I am using VS 2012 and VB.NET language.
I am getting confused about how to read the file which is in folder of a application itself.
Right now I have given the path as
Dim reader As XmlTextReader = New XmlTextReader("C:\mailpara.xml")
but its hard-coded , but I want to make a folder in app. and want to read from that
folder itself.
I want to know how to read the file from the folder of a application.
How to read the file after installation on client's machine and how to add the file while making the set up ?
Use something like;
Dim directory as String = My.Application.Info.DirectoryPath
Dim reader As XmlTextReader = New XmlTextReader(directory & "\MyFolderName\mailpara.xml")
You can use Application.StartupPath property to retrieve the startup path of the application.
Dim reader As XmlTextReader = New XmlTextReader(Application.StartupPath & "mailpara.xml")
You might want to add a check to ensure that the path ends with a \ (I think it may or may not be present depending on whether the path is a root folder or not).