Hack Forums
[.NET] Get Flag From IP [UPDATED] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Get Flag From IP [UPDATED] (/showthread.php?tid=4874028)



Get Flag From IP [UPDATED] - Vysse - Jun 20, 2015

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.


* - The-One - Jun 21, 2015

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 :).


* - Odus - Jun 21, 2015

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



* - Only3 - Jun 21, 2015

(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 😧


* - Vysse - Jun 21, 2015

(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.


* - VmX5 - Jun 23, 2015

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