WebClient downloads an incomplete file - vb.net

This method that I wrote was working fine a week ago but now it downloads an incomplete file. The original file is nearly 10mb but the file that is being downloaded is 2k. My code is basically this
Dim URL as string = "http://www.cqc.org.uk/sites/default/files/cqc_locations_export.csv"
Dim path as string = "C:\temp"
Dim webClient As New WebClient
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)")
webClient.DownloadFile(URL, path)
Any idea what is going wrong here ?
Cheers

This is not my area, but you may be missing the size of the file, if it's binary, char encoding and other header data. Also, this 2k of file maybe a part of the file or metadata.

The Second argument to DownloadFile requires a full filename not just a path (and the root of the C:\ drive is protected on windows 7 so the method may throw an exception if you try to write here)
Note: Don't forget to dispose of the webclient when done.
Note 2: I would suggest you should avoid naming collisions by using names other than the .NET class names. Don't forget VB is case insensitive (unlike C#)
The following works fine for me:
Dim URL As String = "http://www.cqc.org.uk/sites/default/files/cqc_locations_export.csv"
Dim filename As String = "C:\temp\temp.csv"
Using wc As New WebClient
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)")
wc.DownloadFile(URL, filename)
End Using

It could be a problem with the site that is hosting the file, or how that site handles GET requests. I had the same problem downloading a URI I had shared from DropBox. The file was about 3 MB, but only about the first 112 KB was downloaded. When I downloaded the same file from Screencast.com, the whole file was downloaded.

On ground you may want to try three things :
Run your application with Admin rights as C drive is protected in
Windows 7 and above.
Temporary stop your antivirus program and try downloading again.
Only dispose webClient when download is complete (On DownloadFileCompleted Event)

The docs seems to suggest either (two different variations)
Dim client As New WebClient()
or
Dim client As WebClient

Related

NSE ACCESS DENIED

I created a basic coding in visualbasic to download from NSE website.
While the coding still downloads the previous years data, it gives an download error for the current new year.
The RAW URL is https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm If you choose a date (say today) and then select BHAVCOPY report, the site will provide you with a link to download the csv.zip file.
However, if you click on the link directly (https://www.nseindia.com/content/historical/EQUITIES/2017/JAN/cm02JAN2017bhav.csv.zip), the URL returns an error: Access Denied
You don't have permission to access "THE LINK" on this server.
Reference #18.11367a5c.1483362327.35d38c1b
What might be the problem with change in year?
i also facing same issue. fixed by adding 2 http header property.
"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"
"Referer" : "https://www1.nseindia.com/products/content/equities/equities/archieve_eq.htm"
After a bit tweaking I noticed it was something to do with browser. Blocked the cookies and everything is working fine.

FTP Directory browsable in Internet Explorer but not in Windows Explorer; VB.NET code to fake web browsing

I have a strange case where a client's FTP server is fully browsable in a web browser, but not in a file explorer.
This is what I see in IE:
And this is what I see in Windows Explorer:
What I'm really trying is to write code that reads the list of files from this ftp directory:
Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ftpServer), FtpWebRequest)
ftpRequest.Credentials = New NetworkCredential(ftpServerUsername, ftpServerPassword)
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse(), FtpWebResponse)
Dim ftpResponseStream As Stream = ftpResponse.GetResponseStream()
Dim ftpResponseStreamReader As StreamReader = New StreamReader(ftpResponseStream)
Console.WriteLine(ftpResponseStreamReader.ReadToEnd())
ftpResponseStreamReader.Close()
ftpResponseStream.Close()
ftpResponse.Close()
But the code fails with a 451 error:
The remote server returned an error: (451) Local error in processing.
(Details: 451 requested action aborted: local error in processing)
Questions:
Why is the FTP browsable on IE but now in Windows? Should I tell my
client to change some properties on the FTP setup to make it
directory-browsable in Windows?
Is (1) necessary? Instead is it
possible to add/change my code to imitate web-browsing so that the
list of files can be read?
CiarĂ¡n's comment helped access files via Windows Explorer: the URL of the format ftp://username:password#IPAddress/ worked.
For the code, however, a slash "/" at the end of the URL did the trick!
I changed the directory name from ftp://server/directory to ftp://server/directory/ and BOOM! VB was able to retrieve the list of files!
I tried the same in IE, and here's what I get:
ftp://193.XX.XX.XX/flog:
ftp://193.XX.XX.XX/flog/: (note the "/" at the end of directory name)
Anyone else stumbling here with a (451) Local error in processing can try this and see if it helps!
Additional Note:
The URL of the format ftp://username:password#IPAddress/ (again, note the ending "/") also works in code. With this, you can skip the line ftpRequest.Credentials = ....

Can I use Background Intelligent Transfer Service with FTP?

I am writing an application that gets its data from many small servers (the "servers" are data loggers and the app consumes the data from the loggers). The data exists as files on the server and I have been using SSH FTP to get the files. Specifically I'm using the .NET wrapper for WinSCP
I have had some problems with this, some transfer fail (while Filezilla succeeds) and it does not report progress. Also, the downloads can be long due to limited bandwidth at the server. So I would like to use BITS to do the transfers but it appears this only works with HTTP. I could switch to HTTP but I currently check what needs to be downloaded by comparing file sizes and dates on the server and the local cache. This does not seem possible using HTTP.
Is there a way to use BITS and FTP? Or is there a way to check what needs downloading with HTTP?
I have complete control over the servers. They currently run Linux and OpenSSH to facilitate the transfers. I'm using VB and .NET framework 4.0 for the application.
Thanks.
You can use a System.Net.HttpWebRequest to the web URL with a request method of "HEAD". You can then get a System.Net.HttpWebResponse and check the ContentLength and LastModified properties of the response to compare to the local cache.
e.g.
dim request as System.Net.HttpWebRequest
request=System.Net.HttpWebRequest.Create("http://...")
request.Method="HEAD"
dim response as System.Net.HttpWebResponse=request.GetResponse
dim fileLength as integer=response.ContentLength
dim fileDate as datetime=response.LastModified
obviously, you need to trap for exceptions as the GetResponse might not like it if the file isn't there or the server isn't available, etc.
Hope this helps, Regards, Denver

Uploading files from .exe to server

I wanted to know how I would be able to upload files to my server from my WinForms .exe application. I tried: My.Computer.Network.UploadFile(myFile, myServer) but I get this error 'The remote server returned an error: (404) Not Found.' Anyone know another way of uploading files to my server from a .exe? Thanks.
I'm using VB and .Net Framework 4
Here is another way, but it sounds like you are uploading to the wrong page (HTTP error = 404). Check your URL.
Dim fileName As String = AskUserForFileName()
Dim web As New WebClient()
web.UploadFile(uriString, fileName)

Silverlight 4 - Save xml file in Client Side with Elevated Trust / Out Of Browser

I am trying to save an xml file in Client side with Elevated Trust / Out Of Browser mode.. But it's not working....
Please find the following code
XDocument doc = XDocument.Load(new Uri("MyFile.xml",UriKind.RelativeOrAbsolute).ToString());
doc.Element("Books").Element("Book").AddAfterSelf(
new XElement("Cateogry",
new XAttribute("name", "Programming")));
XmlWriter writer = doc.CreateWriter();
doc.Save(writer);
Am I doing some thing wrong here
Thanks
Deepu
If your application has elevated trust see this example:
http://forums.silverlight.net/forums/p/203442/475894.aspx
This will not work on a mac though. If you want to save a file that will work on both windows and mac use the isolated storage.
I hope this helps.