Google 距离矩阵最多可以接受 100 个参数。但是使用 GET Ruquest url lenth 将其限制为 < 15(我认为 2048 个字符),并且我得到了错误 414 - 请求的 URL 太大而无法处理。因此我得出结论,必须使用 POST 方法。但我不能。我收到了 REQUEST_DENIED 错误。那么我该如何使用这项服务呢?
public static bool GetMatrix(string origins, string destinations)
{
string poststring = string.Format("origins={0}&destinations={1}&mode=bicycling&language=fr-FR&sensor=false", origins, destinations);
byte[] postdata = Encoding.UTF8.GetBytes(poststring);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml");
webRequest.Method = "POST";
webRequest.ContentType = "application/xml"; // or any other type dont work
webRequest.ContentLength = postdata.Length;
using (Stream writer = webRequest.GetRequestStream())
writer.Write(postdata, 0, postdata.Length);
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
//Only for debug
using (var stream = new StreamReader(webResponse.GetResponseStream()))
System.Diagnostics.Debug.WriteLine(stream.ReadToEnd());
return (webResponse.StatusCode == HttpStatusCode.OK);
}
}