I am using googlemaps apis to get location name using lat and long provided by user it works on local machine but the problem is that it doesn't work when application is deployed. It gives null exception here is my code.
public static string GETGoogleAddress(double lat, double longi)
{
string URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + longi+ "&key=AIza*****-Lyb****_********efVUW1o7B****";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
JObject parseJson = JObject.Parse(reader.ReadToEnd());
var getJsonres2 = parseJson["results"][1];
var getAddress2 = getJsonres2["formatted_address"];
string _Address = getAddress2.ToString();
return _Address;
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
I am calling this utility helper like this
var _location = UtilityHelper.GeoAddressUtilHelper.GETGoogleAddress(model.Latitude??0, model.Longitude??0);
which doesn't return any location in production. It works fine on local but not working windows server when application is deployed.
Following is the stackTrace i am getting
at Offey.services.UtilityHelper.GeoAddressUtilHelper.GETGoogleAddress(Double lat, Double longi)
Exception Type
NullReferenceException
Exception message
Object reference not set to an instance of an object.