Uploading multiple files with VB.Net - vb.net

I need to upload multiple files in a winforms application. So far I've been using a webclient and while this is working fine I'm wondering if there is a better way for doing this. Does the webclient make a new connection for every request or does the connection persists between uploads?
Dim Ftpclient As New System.Net.WebClient()
Ftpclient.Credentials = New System.Net.NetworkCredential(username, password)
Dim Files As New Dictionary(Of String, String)
''//Fill dictionary with items for upload here
For Each RemoteFile As String In Files.Keys
Ftpclient.UploadFile(RemoteFile, Files(RemoteFile))
Next

It'll create a new TCP connection for every file, because it's way HTTP works.
IMHO, not a bad thing in your scenario.

It depends on what protocl you are using for upload. If you are using HTTP, then the client will reuse the previous connection if it can. I am not sure about FTP - I think FTP also supports keep-alive.
In any case, if you are concerned about performance, you should use Wireshark to see how the connection usage is being accomplished. Is it creating a new connection everytime?

Related

Is there a simple way to use VB.NET to read a SharePoint Online file without logging in?

I inherited a program written in VB.NET. I want to host the installer and documentation for the program in a SharePoint Online library. The SPO library allows View/Read-only access to "Everyone except external users" but it does not allow anonymous access. I want the program to check the SPO library for an updated version when it launches.
I envisioned a simple function like this:
Private Function getVersion() As String
Using client As New WebClient
getVersion = client.DownloadString("https://companyname.sharepoint.com/site/library/version.txt")
End Using
End Function
where version.txt contains nothing but the current version number.
However, this function throws an IOException stating that the connection was forcibly closed by the remote host. I think this is because the SPO site requires authentication.
I don't want to add a user login step solely for this one thing. This probably means an SPO site that requires authentication is not the ideal place for my version.txt file to reside, but I'm also trying to avoid solutions that require me to jump through hoops and involve others to get it to work. I'm the only developer for this program, so I'd like to be able to publish an update without having to wait for someone else to do something (like update a web server that I don't have access to).
Suggestions for a simple technique to achieve my goal?
In your Using block, before the line with DownloadString, set the Credentials of your WebClient:
Private Function getVersion() As String
Using client As New WebClient
client.Credentials = CredentialCache.DefaultCredentials
getVersion = client.DownloadString("https://companyname.sharepoint.com/site/library/version.txt")
End Using
End Function
The DefaultCredentials will be the credentials of the currently logged in Windows user.
See the docs on System.Net.WebClient.

Get a CookieContainer from a WebBrowser control?

First of all, excuse me If I misunderstood some concepts, since web requests and cookies are not my fort.
Well, the thing is that I would like to get all the cookies stored in the WebBrowser's session for the current url. This will help me for example after interactively logging in a website, I can retrieve the cookies from the WebBrowser instance to use them in background with the HttpWebRequest class. (the website has security measures like a captcha and a random question, so for me is not viable to try automate it using requests in background to login and store the cookies, I prefer to use a webbrowser)
This is what I tried so far, it seems that it works fine but I'm not sure whether I did it all ok without missing any important thing to take into account, and I think my approach seems a little dirty solution, I mean maybe exists a direct way to get this from the abstraction layer of the WebBrowser control...
<HideModuleName>
Public Module WebBrowserExtensions
<Extension>
<DebuggerStepThrough>
Public Function GetCookieContainer(ByVal wb As WebBrowser) As CookieContainer
Dim uri As Uri = wb.Url
Dim cookieContainer As New CookieContainer()
Dim cookies As String() = wb.Document.Cookie.Split({";"c}, StringSplitOptions.None)
For Each cookie As String In cookies
Dim name As String = cookie.Substring(0, cookie.IndexOf("="c)).TrimStart(" "c)
Dim value As String = cookie.Substring(cookie.IndexOf("="c) + 1)
cookieContainer.Add(uri, New Cookie(name, value, "/", uri.Host))
Next cookie
Return cookieContainer
End Function
End Module
So, my question is: It is all ok with my approach?, if not then which would be the proper way to do this?.
PS: I know the WebBrowser is a embedded instance of Internet Explorer, but I'm not sure about how the WebBrowser manages the cookies, I mean if the cookies persists only during the lifetime of the current application or the WebBrowser instance, or just the cookies are stored in the system and are the same cookies used by Internet Explorer. As I said, excuse me If I misunderstood some concepts.

disposing properly of streamwriters around a tcpclient

my current code has a TCPclient connection set up, i want to write some data (a string: UserName) to it.
using outStream as new streamwriter(client.getstream)
outStream.write(UserName)
end using
my problem with this code is that it disposes of the client when it is finished and I need to keep the client open. if I change the using to a dim and do not dispose of the streamwriter, the data does not actually get written and so i want to know whether how i can send the data and keep the client open.
Closing a StreamWriter will close the connection stream. .NET 4.5 added an overload to the StreamWrite that gives the ability to keep the stream opened.
Is there any way to close a StreamWriter without closing its BaseStream?
An other option is to not use Using and sticking with a Dim and closing the stream only at the end. If you want to force the data to be sent, you'll have to call flush.
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter.flush

how to hide http request using vb.net

I'm developing new app. This is app need to get information from my website so I use HTTP request using vb.net.
Sub Main()
'Address of URL
Dim URL As String = http://whatever.com
' Get HTML data
Dim client As WebClient = New WebClient()
Dim data As Stream = client.OpenRead(URL)
Dim reader As StreamReader = New StreamReader(data)
Dim str As String = ""
str = reader.ReadLine()
Do While str.Length > 0
Console.WriteLine(str)
str = reader.ReadLine()
Loop
My problem is , I found an app called fiddler http://www.fiddler2.com/fiddler2/
This app could find all HTTP request that maked using my app , so this put my website at risk.
Is There any way how to hide HTTP request from been detected ????
I'm not familiar with this "fiddler" product, but just from reading the page that you reference, it is something that runs on the user's machine and monitors traffic between that computer and the Internet. I don't see how it would be even theoretically possible to prevent this. Signals are moving over wires leaving the user's computer. He presumably has physical access to his own computer. If nothing else, he could attach something to the cable coming out the back of the computer that monitors the signals moving over the wire.
You could encrypt messages so that it's difficult for the user to interpret what they mean, but you can't stop him from reading the message as it was sent.
I wonder, by the way, how it is a security problem for a user to know what messages are being sent from his own computer. Are you trying to hide what you are doing from the person using your program? Frankly, if I discovered that an application I have on my computer was trying to hide what it was doing from me, I would immediately delete it. Why would someone want to hide what he's doing to MY computer unless what he is doing is something sinister, trying to steal my personal data or some such?
Just to be slightly sarcastic, your question sounds a little like asking, When I visit a business associate, how can I prevent him from finding out out what I did in his office when he stepped out for a few minutes?
There is a way using the Proxy method just add this Code in the HTTP request:
Request.Proxy = New WebProxy()
cause how fiddler works it sets itself as the proxy using the above code it cannot set fiddler proxy and thats how you stop it from reading anything

Track installs of software

Despite my lack of coding knowledge I managed to write a small little app in VB net that a lot of people are now using. Since I made it for free I have no way of knowing how popular it really is and was thinking I could make it ping some sort of online stat counter so I could figure out if I should port it to other languages. Any idea of how I could ping a url via vb without actually opening a window or asking to receive any data? When I google a lot of terms for this I end up with examples with 50+ lines of code for what I would think should only take one line or so, similar to opening an IE window.
Side Note: Would of course fully inform all users this was happening.
Just a sidenote: You should inform your users that you are doing this (or not do it at all) for privacy concerns. Even if you aren't collecting any personal data it can be considered a privacy problem. For example, when programs collect usage information, they almost always have a box in the installation process asking if the user wants to participate in an "anonymous usage survey" or something similar. What if you just tracked downloads?
Might be easier to track downloads (assuming people are getting this via HTTP) instead of installs. Otherwise, add a "register now?" feature.
You could use something simple in the client app like
Sub PingServer(Server As String, Port As Integer)
Dim Temp As New System.Net.Sockets();
Temp.Connect(Server, Port)
Temp.Close()
End Sub
Get your webserver to listen on a particular port and count connections.
Also, you really shouldn't do this without the user's knowledge, so as others have said, it would be better to count downloads, or implement a registration feature.
I assume you are making this available via a website. So you could just ask people to give you their email address in order to get the download link for the installer. Then you can track how many people add themselves to your email list each month/week/etc. It also means you can email them all when you make a new release so that they can keep up to date with the latest and greatest.
Note: Always ensure they have an unsubscribe link at the end of each email you send them.
The guys over at vbdotnetheaven.com have a simple example using the WebClient, WebRequest and HttpWebRequest classes. Here is their WebClient class example:
Imports System
Imports System.IO
Imports System.Net
Module Module1
Sub Main()
' Address of URL
Dim URL As String = http://www.c-sharpcorner.com/default.asp
' Get HTML data
Dim client As WebClient = New WebClient()
Dim data As Stream = client.OpenRead(URL)
Dim reader As StreamReader = New StreamReader(data)
Dim str As String = ""
str = reader.ReadLine()
Do While str.Length > 0
Console.WriteLine(str)
str = reader.ReadLine()
Loop
End Sub
End Module
.NET? Create an ASMX Web Service and set it up on your web site. Then add the service reference to your app.
EDIT/CLARIFICATION: Your Web Service can then store passed data into a database, instead of relying on Web Logs: Installation Id, Install Date, Number of times run, etc.