In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Background: the blogger is a Windows desktop application development engineer with little knowledge of network communications. One day, after visiting a treasure, my wife asked, "Why do they know my address? how did they get my address?"
With this question, our exploration begins:
Step 1: simple service building
The idea is to build a simple server locally through HttpListener, the development program is the console interface, and the core class HttpListenerService:
Method Start () opens the thread pool to listen for the specified IP, and the local port selects 9527 (the serial number of Zhou Xingchi Tang Pak Hu Qiu Xiang in Washington)
Public void Start (object obj) {try {/ / specify authentication Anonymous anonymous access _ httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; / / GetLocalIpAddress ()-get local IP method _ httpListener.Prefixes.Add ("http://" + GetLocalIpAddress () +": 9527 / ") _ httpListener.Start (); / / wait for request connection while (true) {/ / wait for request connection / / GetContext is blocked if there is no request var ctx = _ httpListener.GetContext () ThreadPool.QueueUserWorkItem (TaskProc, ctx);}} catch (Exception ex) {throw;}}
Step 2: get the IP address of the client request
After querying many examples on the Internet, I feel too complicated. Through the quick monitoring function of VS, I found that there is an attribute RemoteEndPoint that can obtain the client IP and port number. In fact, it should be distinguished according to Get and POST, in order to facilitate temporary omission.
1 private void TaskProc (object o) 2 {3 try 4 {; 5 var ctx = (HttpListenerContext) o; 6 string ipAddress = null; 7 if (ctx.Request.RemoteEndPoint! = null) 8 {9 ipAddress = ctx.Request.RemoteEndPoint.Address.ToString () 10} 11 / / do not look at the following for the time being. You have obtained the client access IP12 var encoding = Encoding.GetEncoding ("gb2312"); 13 var callbackData = JsonConvert.SerializeObject (IpGetCity (GetOuterNetIp (); 14 var data = encoding.GetBytes (callbackData); 15 ctx.Response.StatusCode = 200 16 ctx.Response.Close (data, false); 17} 18 catch (Exception ex) 19 {20} 21}
The third step: how to obtain the address of the client according to the IP of the client. Because the matter originated from Taobao, we queried whether Taobao has a similar interface. Sure enough, we found the Json object of the http://ip.taobao.com/service/getIpInfo.php?ip= customer IP, parsed the data through Fiddler, created the corresponding Json format object, and encapsulated the acquisition method.
1 public static Result IpGetCity (string ipAddres) 2 {3 try 4 {5 if (! ValidateIpAddress (ipAddres)) return null; 6 7 var url = new Uri ("http://ip.taobao.com/service/getIpInfo.php?ip=" + ipAddres); 8 var request = (HttpWebRequest) WebRequest.Create (url); 9 request.Timeout = 1000 years 5 10 request.KeepAlive = false;11 request.Method = "GET"; 12 var resoponse = request.GetResponse (); 13 string pageHtml;14 / / ReSharper disable once AssignNullToNotNullAttribute15 using (var stream = new StreamReader (resoponse.GetResponseStream () 16 {17 pageHtml = stream.ReadToEnd () 18} 19 var data = JsonConvert.DeserializeObject (pageHtml); 20 return data;21} 22 catch (WebException webEx) 23 {24 throw;25} 26}
Step 4: test. Because the local area network is a local area network, the return must be empty after sending the IP address, so first try to query the local public network IP, so how to get the local public network IP? Or the same as the previous idea, the network query similar interface, Baidu directly based on the first search result query, http://1212.ip138.com, establish webrequest request, grab Html data
1 public static string GetOuterNetIp () 2 {3 var tempIp = ""; 4 try 5 {6 WebRequest wr = (HttpWebRequest) WebRequest.Create ("http://1212.ip138.com"); 7 var stream = wr.GetResponse () .GetResponseStream (); 8 var sr = new StreamReader (stream, Encoding.GetEncoding (" gb2312 ")) 9 var all = sr.ReadToEnd (); 10 / read data from the website 11 var start = all.IndexOf ("your IP is: [, StringComparison.Ordinal) + 710 12 var end = all.IndexOf ("] ", start, StringComparison.Ordinal); 13 tempIp = all.Substring (start, end-start); 14 sr.Close () 15 stream.Close (); 16} 17 catch28 {19 / / ignored20} 21 return tempIp;22}
But the question arises, why the data obtained by my browser can get the native IP, but the request for the connection I set up is like this.
After analysis, it is found that the URL is processed by the iframe framework, and the internal information of the frame framework cannot be obtained. Then grab the frame information and find that the path he really requested is http://1212.ip138.com/ic.asp, and the coding format of the website is gb2312. Send a request again to get the returned data:
Although I know something about it, I still need to continue to explore a series of problems, such as user access with a proxy server.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.