Last active
November 27, 2023 18:48
-
-
Save unwiredlabs/9543100 to your computer and use it in GitHub Desktop.
.NET C# API sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///Contributed by Stuart Eastland of RightFile.com | |
public static void GetNearest(string key, string mcc, string mnc, string lac, string cid) | |
{ | |
try | |
{ | |
string url = "http://us1.unwiredlabs.com/v2/process.php"; | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); | |
httpWebRequest.ContentType = "application/json; charset=utf-8"; | |
httpWebRequest.Method = "POST"; | |
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) | |
{ | |
string json = string.Format("{{ \"token\": \"{0}\", \"radio\": \"gsm\", \"mcc\": {1}, \"mnc\": {2}, \"cells\": [{{ \"lac\": {3}, \"cid\": {4} }}] }}", key, mcc, mnc, lac, cid); | |
streamWriter.Write(json); | |
streamWriter.Flush(); | |
} | |
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
string json = streamReader.ReadToEnd(); | |
var results = JsonConvert.DeserializeObject<dynamic>(json); | |
var status = results.status.Value; | |
if (status == "ok") | |
{ | |
var balance = results.balance.Value; | |
var lat = results.lat.Value; | |
var lon = results.lon.Value; | |
var accuracy = results.accuracy.Value; | |
} | |
else | |
{ | |
var message = results.message.Value; | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// handle exception | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment