Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Get Flag From IP [UPDATED]

So I needed a working version of the get flag from IP so I updated the code from ubbelol's post years ago. Here is the code. Credits to ubbelol for the original code.

Code
Private Function retFlag(ByVal IP As String) As Image
      Dim srcReq As WebRequest = WebRequest.Create("http://geoiptool.com/en/?IP=" & IP)
      Dim srcRes As WebResponse = srcReq.GetResponse()
      Dim src As String = New StreamReader(srcRes.GetResponseStream()).ReadToEnd()
      Dim imgLoc As String = Regex.Split(Regex.Split(src, "<img src=""")(3), """")(0)
      imgLoc = imgLoc.Remove(0, 2)
      Dim picReq As WebRequest = WebRequest.Create(String.Concat("http://geoiptool.com", imgLoc))
      Dim picRes As WebResponse = picReq.GetResponse()
      Return Image.FromStream(picRes.GetResponseStream())
    End Function

For those wondering why I used String.Remove instead of Regex the reason is Regex wasn't working properly for what I wanted to do and I couldn't figure out why so I went with a method I knew would work.
Thanks for sharing with HF. Just a side note; your naming, 'retFlag' should be something more clear.
Also, String.Split is MUCH faster then Regex.Split when using small strings and simple patterns :).
FR1908
Cleaned it up a little:

Code
Private Function FetchFlag(ip As String) As Image
    Using wc As New WebClient
      Dim countryCode As String = wc.DownloadString("http://ip-api.com/csv/" & ip).Split(","c)(2).ToLower()

      Using ms As New MemoryStream(wc.DownloadData(String.Format("https://geoiptool.com/static/img/flags/{0}.gif", countryCode)))
      Return Image.FromStream(ms) 'for Only3
      End Using
    End Using
End Function
(Jun 21, 2015 08:56 AM)Odus Wrote:  Cleaned it up a little:

Code
Private Function FetchFlag(ip As String) As Image
    Using wc As New WebClient
      Dim countryCode As String = wc.DownloadString("http://ip-api.com/csv/" & ip).Split(","c)(2).ToLower()
      Dim ms As New MemoryStream(wc.DownloadData(String.Format("https://geoiptool.com/static/img/flags/{0}.gif", countryCode)))

      Return Image.FromStream(ms)
    End Using
End Function

😊 Stream Dispose 😧
(Jun 21, 2015 08:56 AM)Odus Wrote:  Cleaned it up a little:

Code
Private Function FetchFlag(ip As String) As Image
    Using wc As New WebClient
      Dim countryCode As String = wc.DownloadString("http://ip-api.com/csv/" & ip).Split(","c)(2).ToLower()

      Using ms As New MemoryStream(wc.DownloadData(String.Format("https://geoiptool.com/static/img/flags/{0}.gif", countryCode)))
      Return Image.FromStream(ms) 'for Only3
      End Using
    End Using
End Function

Cleaned it. You rewrote it. Nice job I had no idea about ip-api.com.
Replace INSERT LINK 1 HERE with : htt3p://ip-api.co3m/csv/
Remove the 3's from the link above!
Replace INSERT LINK HERE 2 with : http3s://geoiptool.co3m/static/img/flags/
Remove the 3's from the link above!
*SORRY I CANNOT POST LINKS YET!

*Function :

Code
Public Function GetFlag(ByVal ip As String)
      Dim wbx As WebClient = New WebClient
      Dim ipstring As String = wbx.DownloadString("INSERT LINK 1 HERE!" & ip)
      Dim country As String = ipstring.Split(","c)(1)
      Dim countrycode As String = ipstring.Split(","c)(2)
      Return ("INSERT LINK HERE 2" & countrycode.ToString.ToLower & ".gif").ToString
    End Function

*Calling Function

Code
PictureBox1.ImageLocation = GetFlag("216.58.218.174") 'Google.com IP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Recycle Bin.Nov 6, 2015 09:36 PM 61 6,298 Nov 6, 2015 09:36 PM
Last Post: TheUnCola
[Shell] by AMDjackJun 25, 2012 01:02 PM 9 271 Jun 25, 2012 01:02 PM
Last Post: AMDjack
[.NET] by ScumMar 4, 2012 01:26 AM 8 180 Mar 4, 2012 01:26 AM
Last Post: tokzikate