Get Requested URL - vb.net

I have been trying to read any requested URLs to my localhost. I think the simplest way to do this is to use the TCPListener. Below is what I've built so far but I'm not sure how I can read the URL requested as a string. I actually want to take the first incoming URL, parse out the data I need then shut the TCPlistener down. Any idea how I can get the URL?
Dim TClient As New TcpListener(Net.IPAddress.Parse("127.0.0.1"), 80)
TClient.Start()
Dim gotIt As Boolean = False
Do While gotIt = False
Dim x = TClient.AcceptTcpClient()
Console.WriteLine(x)
TClient.Stop()
Loop

Using an example from the MSDN I was able to come up with this:
Dim server As TcpListener = Nothing
Dim port As Int32 = 80
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
server.Start()
Dim bytes(1024) As Byte
Dim data As String = Nothing
Dim client As TcpClient = server.AcceptTcpClient
data = Nothing
Dim stream As NetworkStream = client.GetStream
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
client.Close()
server.Stop()
The data variable contains the information I am looking for, just have to parse it out.
Thank you, #CoderDennis

What you're looking for is the Referer header.
String requestedUrl = request.getHeader("Referer");
Mind that Referer is case sensitive.
100% working!

Related

VB.NET TCP Server Receiving Data Properly

I have a multithread(with Backgroundworker) TCP Server which is developed by VB.NET. It works very good when client and server at same machine. But when i connect to server from another computer which is at same LAN it works different. At this case it works good until i start to send messages consecutively (3-4 messages at a second). I send :
Hi
Hi
Hi
Hi
Hi
and server gets this messages such :
Hi
HiHi
HiHiHi
Hi
etc.
It's very interesting that this issue occurs only when client is at other computer at LAN.
Here is my listen Sub :
Sub listen_port6(ByVal b As BackgroundWorker)
Dim server As TcpListener
server = Nothing
Try
Dim port As Int32 = 8085
server = New TcpListener(IP, port)
server.Start()
Dim bytes(1024) As Byte
Dim data As String = Nothing
While True
Dim client As Sockets.TcpClient = server.AcceptTcpClient()
Dim ipend As Net.IPEndPoint = client.Client.RemoteEndPoint
PublicIP = ""
If Not ipend Is Nothing Then
PublicIP = ipend.Address.ToString
End If
b.ReportProgress(1)
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
Dim k As Short
For k = 0 To 1024
bytes(k) = 0
Next
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
data = ""
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Debug.Print(data)
i = stream.Read(bytes, 0, bytes.Length)
End While
client.Close()
End While
Catch errore As Exception
Error_Print(errore.Message)
Finally
server.Stop()
End Try
End Sub
Thanks in advance

For each ip try a list of urls

Now i have:
Dim i As Integer
Dim web As New WebClient With {.Proxy = Nothing}
'http://1.2.3.4/sql.db
Dim attkstring As String = "/sql.db"
i = 0
Dim shellx As Integer = 0
Dim sUrl As String = shells.Items(shellx) & attkstring
For Each item In shells.Items
Try
If web.OpenRead(sUrl) = True Then
End If
Catch ex As Exception
End Try
shellsloadedtext.Items.Add(sUrl)
shellx += 1
Next
But in my ListBox write all (ip+url), I need to write the IP + the URL just in case work, and now writes them even if there are no
Ex: real ip: 7.7.7.7 false ip: 1.1.1.1
I write in the ip list 7.7.7.7 1.1.1.1
And my ListBox shellsloadedtext wirte 7.7.7.7/sql.db and 1.1.1.1/sql.db
Help me
Does this help?
Dim sUrl As String = ips.Items(urlchk) & urlstring
web.DownloadString(sUrl)
yourListBox.Items.Add(sUrl)
Reference:
HTTP GET with .NET WebClient

automatically download a report

This is the code that i have made but now working to save the report to the directory:
As you see i follow pretty much a lot of microsoft tutorials of how use this class of reporting service, but still dont get how get it working
'objetos de reporting
Dim rs As New reportingservice.ReportingService2010
Dim rsExec As New ReportExecution.ReportExecutionService
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
'datos generales
Dim historyID As String = Nothing
Dim deviceInfo As String = Nothing
Dim format As String = "PDF"
Dim results As Byte()
Dim encoding As String = String.Empty
Dim mimeType As String = String.Empty
Dim extension As String = String.Empty
Dim warnings As ReportExecution.Warning() = Nothing
Dim streamIDs As String() = Nothing
Dim filename As String = "C:/Users/gdedieu/Desktop/reporte.pdf" ' Change to where you want to save
Dim _reportName As String = "per_anexo_1"
Dim _historyID As String = Nothing
Dim _forRendering As Boolean = False
Dim _values As ReportExecution.ParameterValue() = Nothing
Dim _credentials As reportingservice.DataSourceCredentials() = Nothing
Dim ei As ReportExecution.ExecutionInfo = rsExec.LoadReport(_reportName, historyID)
'definimos el parĂ¡metro
_values(0).Name = "an1_id"
_values(0).Value = 1
rsExec.SetExecutionParameters(_values, "en-us")
results = rsExec.Render(format, deviceInfo, extension, mimeType, encoding, warnings, streamIDs)
Dim stream As New System.IO.FileStream(filename, IO.FileMode.OpenOrCreate)
stream.Write(results, 0, results.Length)
stream.Close()
Try setting up a subscription via the Report Manager and specifying the Report Delivery Options value for 'Delivered By:' as 'Report Server File Share'.
This lets you specify a path for a report file to be written to - you will need to ensure that the Reporting Services server has write access to the destination.

Google Maps API V3 Returns 610,0,0,0

I am using Google Maps API V3 on my vb.net 2010 application.I tested http://maps.google.com/maps/geo?output=csv&key=MYAPIKEY&q= 46 long street, kempton park directly on the web browser and I get 610,0,0,0 error. the whole point of my appllication is to get gps coordinates from the address and also get address from given gps coordinates. i tried generating a new browser key but is still not working. Thanks in advance. Please refer to the code below:
Can someone please advise why is Google Maps API returning this 610,0,0,0 error
Public Function GetLatLon(ByVal addr As String) As LatLon
Dim url As String = "http://maps.google.com/maps/geo?output=csv&key=MYAPIKEY" & addr
Dim request As System.Net.WebRequest = WebRequest.Create(url)
Dim response As HttpWebResponse = request.GetResponse()
If response.StatusCode = HttpStatusCode.OK Then
Dim ms As New System.IO.MemoryStream()
Dim responseStream As System.IO.Stream = response.GetResponseStream()
Dim buffer(2048) As Byte
Dim count As Integer = responseStream.Read(buffer, 0, buffer.Length)
While count > 0
ms.Write(buffer, 0, count)
count = responseStream.Read(buffer, 0, buffer.Length)
End While
responseStream.Close()
ms.Close()
Dim responseBytes() As Byte = ms.ToArray()
Dim encoding As New System.Text.ASCIIEncoding()
Dim coords As String = encoding.GetString(responseBytes)
Dim parts() As String = coords.Split(",")
Return New LatLon(Convert.ToDouble(parts(2)), Convert.ToDouble(parts(3)))
End If
Return Nothing
End Function
you don't need take a look of HTML source code
but here...
https://maps.googleapis.com/maps/api/geocode/xml?address=terracina,via+roma+100&sensor=false
i userd with my address
take a look
it is a google service, and you can get the XML's location
cheers

How to get my real IP using vb.net?

How to get my real IP using vb.net?
If you are running in ASP.NET, then use the HttpRequest.UserHostAddress property:
Dim ip as string
ip = Request.UserHostAddress()
Create php script. Save it as realip.php
<?php
echo $this->getRealIpAddr();
function getRealIpAddr()
{
$ip = "";
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
?>
In your VB.net project create a module.
Declare the imports section at the very top
Imports System.Net
Imports System.IO
And create your function:
Public Function GetIP() As String
Dim uri_val As New Uri("http://yourdomain.com/realip.php")
Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim myIP As String = reader.ReadToEnd()
response.Close()
Return myIP
End Function
Now anywhere in your code you can issue
Dim myIP as String = GetIP()
as use the value from there as you wish.
As you can see here (how and why), the best way to get the client IP is:
Dim clientIP As String
Dim ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If Not String.IsNullOrEmpty(ip) Then
Dim ipRange As String() = ip.Trim().Split(","C)
Dim le As Integer = ipRange.Length - 1
clientIP = ipRange(le)
Else
clientIP = Request.ServerVariables("REMOTE_ADDR")
End If
Dim req As HttpWebRequest = WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")
Dim res As HttpWebResponse = req.GetResponse()
Dim Stream As Stream = res.GetResponseStream()
Dim sr As StreamReader = New StreamReader(Stream)
MsgBox(sr.ReadToEnd())