-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
executable file
·73 lines (65 loc) · 2.61 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace IPTrack
{
public partial class frmMain : Form
{
GeoIP test = new GeoIP();
public frmMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSubmit_Click(object sender, EventArgs e) {
try {
test.IP = IPAddress.Parse(txtIP.Text);
test.GetLocation();
lblCity.Text = "City: " + test.City;
lblCountry.Text = "Country: " + test.Country;
lblLatitude.Text = "Latitude: " + test.Latitude;
lblLongitude.Text = "Longitude: " + test.Longitude;
if (test.Longitude != 361) {
float xplot = ((float)imgMap.Width / 360) * (180 + test.Longitude);
float yplot = ((float)imgMap.Height / 180) * (90 - test.Latitude);
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.White, 1);
System.Drawing.Graphics formGraphics = imgMap.CreateGraphics();
imgMap.Refresh();
formGraphics.DrawLine(myPen, xplot, 0, xplot, imgMap.Height);
formGraphics.DrawLine(myPen, 0, yplot, imgMap.Width, yplot);
}
}
catch (Exception ex){
MessageBox.Show(ex.Message);
};
}
}
public class GeoIP {
public IPAddress IP;
public string Country;
public string City;
public float Latitude=361;
public float Longitude=361;
public void GetLocation(){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.hostip.info/get_html.php?position=true&ip=" + IP);
request.Method = "GET";
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
Country = sr.ReadLine().Substring(9);
City = sr.ReadLine().Substring(6);
sr.ReadLine();
try {
Latitude = float.Parse(sr.ReadLine().Substring(10)); // substrings to skip the "Latitude: "
Longitude = float.Parse(sr.ReadLine().Substring(11)); // and "Longitude: "
}
catch {
};
sr.Close();
response.Close();
}
}
}