我正在使用GoogleMaps服务从提供的地址获取gps坐标,但如果此地址包含一些字符,例如:'(',')'或者'&'然后 WebRequest.Create(地址)方法使用未编码的字符构建一个Uri。我发送的URL已经编码,但看起来像Create方法会破坏某些字符。
执行请求时会产生错误。这就是网址的样子,请注意'('在地址参数中:
GET /maps/api/geocode/xml?address=111%CE%BF%20%CF%87%CE%BB%CE%BC%20%CE%91%CE%B3%CF%81%CE%B9%CE%BD%CE%AF%CE%BF%CF%85-%CE%91%CE%BD%CF%84%CE%B9%CF%81%CF%81%CE%AF%CE%BF%CF%85%20(%CE%BA%CF%8C%CE%BC%CE%B2%CE%BF%CF%82%20%CE%91%CE%B3.%20%CE%94%CE%B7%CE%BC%CE%B7%CF%84%CF%81%CE%AF%CE%BF%CF%85)%2C%20%CE%91%CE%B3%CF%81%CE%AF%CE%BD%CE%B9%CE%BF%2C%2030100&sensor=false&client=XXXX&signature=XXXXX&channel=XXXX HTTP/1.1
作为一种时间解决方案,我正在删除这些字符,但我想知道是否有办法在WebRequest对象中构建正确的URL。这是我的代码示例:
private static Tuple<string, string> GetGpsCoordinates(string address)
{
string requestUri =
string.Format("/maps/api/geocode/xml?address={0}&sensor=false",
Uri.EscapeDataString(address));
// here I add some credentials for the Google API
string authRequest = GMapsAuthenticator.GetAuthorisedUrl(requestUri);
try
{
WebRequest request = WebRequest.Create(authRequest);
WebResponse response = GMapsReadThrottler.Run(() => request.GetResponse()); //error
XDocument xdoc = XDocument.Load(response.GetResponseStream());
// DO THINGS ...
}
catch (Exception)
{
throw;
}
}
谢谢你们!!! :)