C# GetResponse with Timeout returning nulls in WinApp
i call following routine when loop thru sql database and try long\lat coordinates for 20-30 customer sites. works fine in debug mode when step thru main loop , stop between each call geocoderlocation routine. when let program run @ full speed, routine sporadically returns nulls. though timeout fix that, not.
thank in advance , forward helping me upgrade routine more reliable , return results.
public class geocoderlocation
{
public double longitude { get; set; }
public double latitude { get; set; }
public override string tostring()
{
return string.format("{0}, {1}", latitude, longitude);
}
}
public static geocoderlocation locate(string query)
{
try
{
webrequest request = webrequest
.create("http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
+ httputility.urlencode(query));
using (webresponse response = request.getresponse())
{
using (stream stream = response.getresponsestream())
{
request.timeout = 5000;
xdocument document = xdocument.load(new streamreader(stream));
xelement longitudeelement = document.descendants("lng").firstordefault();
xelement latitudeelement = document.descendants("lat").firstordefault();
if (longitudeelement != null && latitudeelement != null)
{
return new geocoderlocation
{
longitude = double.parse(longitudeelement.value, cultureinfo.invariantculture),
latitude = double.parse(latitudeelement.value, cultureinfo.invariantculture)
};
}
}
}
messagebox.show("web response null " + query.tostring());
return null;
}
catch (webexception ex)
{
messagebox.show(ex.message, "exception");
return null;
}
}
it taking more 5 seconds. make timeout larger
request.timeout = 5000;
jdweng
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment