NET background access URL to get the return value

Recently, I am doing a query through the Sina IP address query interface to check whether the user's IP is a domestic IP.

1                  string strBuff = "" ;
 2                  string url = " http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip= " + IP;
 3                  Uri httpURL = new Uri(url);
 4                  /// The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be created by the Create method of WebRequest, and the forced type conversion is performed.    
5                  HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
 6                  /// Pass The GetResponse() method of HttpWebRequest establishes HttpWebResponse and casts the type    
7                  HttpWebResponse httpResp = (HttpWebResponse) httpReq.GetResponse();
8                  /// The GetResponseStream() method obtains the data stream of the HTTP response, and tries to obtain the content of the webpage specified in the URL   
 9                  /// If the content of the webpage is successfully obtained, it will be returned in the form of System.IO.Stream, and if it fails, it will be generated ProtoclViolationException error. The correct approach here is to place the following code in a try block. Here is simple processing    
10                  Stream respStream = httpResp.GetResponseStream();
 11                  /// The returned content is in the form of Stream, so you can use the StreamReader class to get the content of GetResponseStream, and use    
the Read method of the StreamReader class to read the web page source in turn The content of each line of program code until the end of the line (read encoding format: UTF8)    13                  StreamReader respStreamReader = new StreamReader(respStream, System.Text.Encoding.UTF8);
 14                  strBuff = respStreamReader.ReadToEnd();
 15 if                 
                 (strBuff != " -2 " )
 16                  {
 17                      strBuff = strBuff.Substring(strBuff.IndexOf( " = " ) + 1 ).TrimEnd( ' ; ' );
 18                      SinaIPAPI model = Newtonsoft.Json.JsonConvert.DeserializeObject<SinaIPAPI > (strBuff);
 19                      if (model.ret == 1 && model.country == " \u4e2d\u56fd " ) // (Unicode code) \u4e2d\u56fd means China 
20                          dal.InsertUserChina(UID, 1);
21                     else if (model.ret == 1 && model.country != "\u4e2d\u56fd")
22                         dal.InsertUserChina(UID, 0);
23                 }                   

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325274082&siteId=291194637