How to track specific client requests on a server? - vb.net

I am trying to track the call for opening a file and then initiating a new thread to handle the request. How is it possible to trap a specific request from a client such as opening a certain file on a server in VB.net ?
I have looked up the WebRequest class. But it doesn't seem to meet my requirements. How can I solve my problem using something similar ?

Write a method that handles file requests.
Sub GetFile(path as string)
'Do something to log the file request
'Now process the file request
End Sub

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.

Soap client acknowledgement

I have created a small web service method using an asmx file. Here'a simplified version of it.
<WebMethod()> _
Public function DeleteFile(Byval fileID As String) as boolean
DeleteFileByID(fileID)
return true
End Sub
It's working very well but I would like to make sure the data sent back to the client doesn't get lost in the process.
I know this could be done by setting a second web service method that would be call by the client to confirm he received some data. However, I would like to know if this could be done in a single web service method.
Here's an example of what I might be looking for:
<WebMethod()> _
Public function DeleteFile(Byval fileID As String) as boolean
return true
clientAcknowledgement = 'This is what I'm loking for... How to make sure the client received the confiormation before deleting the file
if clientAcknowledgement then
DeleteFileByID(fileID)
end if
End Sub
I would solve this by adding a parameter to the web method call that the caller can set.
<WebMethod()> _
Public Function DeleteFile(ByVal fileID As String, clientAcknowledgement As Boolean) As Boolean
If clientAcknowledgement Then
DeleteFileByID(fileID)
End If
End Function
If the developer that calls this method is lazy and just sets it to true all of the time without prompting the end user, then it is their responsibility to answer to their users.
You could get pretty complex with scenarios to call one method then another, but at the end of the day, the developer consuming the web services can circumvent just about anything you put into place and you have no real guarantee that the end user has been asked unless you implement the code yourself.
UPDATE
After clarification that the desire is to know that the caller received the response prior to actually deleting the file, I have some additional ideas.
There is no easy way that I am aware of to ensure the web services response has been successfully delivered to the client before performing the actual deletion.
However, there are a couple of alternatives:
Option A: Add a DeleteRequested method that sends a token to the caller that they then have to return to the DeleteFile method. You would only perform the deletion if the token is valid. There are still timing issues here, but it is slightly better than the current implementation.
Option B: Implement an IHttpHandler that is exposed through an ASHX page. Because you are responsible for sending the response in this scenario, you will know if it completed transferring successfully or not (i.e. the client disconnected), so you could wait to perform your delete until the response completes. This does change how the client calls and responds to your service, but does give you the validation you are after.
Trying to read between the lines a little, it occurs to me that the poster's problem may really be that if something goes wrong in between any of a series of actions (invoked using web service calls), the database or a set of files (or both) might be left in an inconsistent state.
In which case, I would suggest the use of a transaction.
A good starting point on transactions is the MSDN article Distributed Transactions in Visual Basic .NET.
All sorts of things could go wrong with - to use the poster's example - deleting a file, not just the lack of a SOAP confirmation. The idea of a transaction is that if anything goes wrong, all changes made within the transaction are reversed. HTH

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

Best way to communicate between 2 .Net apps?

If I control both applications, what is the best way to communicate between 2 exe's written in VB.Net. For example, I want to drop an XML file from one app, and pick it up with the other, but I do not want poll for the file. I've heard of named pipes, but I found it was complicated. What's the most effecient way to do this?
The easiest way is probably to use Windows Communication Foundation. This article has example code written in VB.NET.
You don't have to poll for the file. Use a FileSystemWatcher.
One simple way would be to use WCF. The receiver application could host a simple WCF service, and the sender could send the file to it.
.NET 4 includes support for memory-mapped files. With these you may even eschew the need to use the filesystem. However, if the processes are not running on the same machine, you'll have to use some other approach (as mentioned by others, WCF would be a good one).
If you can edit the .exe’s file here is the easiest way:
Add a FileSystemWatcher Object in one of the .exe and set a Filter to a Specific file for example “Commands.txt”
FileSystemWatcher1.Path = Application.StartupPath
FileSystemWatcher1.NotifyFilter=NotifyFilters.LastWrite
FileSystemWatcher1.Filter = "Commands.txt"
FileSystemWatcher1.EnableRaisingEvents = True
To star/stop monitoring, set the path and the EnableRaisingEvents property to True or False
This is the Event Raised when the file changes:
Private Sub FileSystemWatcher1_Changed(sender As System.Object, e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'Open the file and read the content.
'you can use your own commands
End Sub
This way, you only will get an event when the file changes, and no need to use timers or anything else.
The other .exe file, just have to write the commands or the message you want to send:
This example writes the current datetime overwriting the file each time.
Dim Timestamp() As Byte = System.Text.Encoding.Default.GetBytes(Now.ToString)
Dim Fs As System.IO.FileStream
Fs = New System.IO.FileStream("Commands.txt", FileMode.Create, FileAccess.Write)
Fs.Write(Timestamp, 0, Timestamp.Length - 1)
Fs.Close()
Done!

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.