我希望以字符串responseString
获取响应。但是当我调试我的代码时,它不起作用,经过一段时间后它会给我带来错误。
操作已超时
以下是代码
string url = String.Format("https://maps.googleapis.com/maps/api/place/autocomplete/json?input={0}&key={1}", "Andheri West".Replace(" ", ","), "AIzaSyA5WiXTvjPSXEMGwFP0olr9vQuHSk-GVjY");
WebClient Webclient = new WebClient();
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
Uri featureUri = new Uri(url);
string responseString = Webclient.UploadString(featureUri, "POST", string.Empty);
答案 0 :(得分:1)
您正在应用POST
请求,但这是错误的。您应该应用GET
请求。所以你可以使用Webclient.DownloadString
string url = String.Format("https://maps.googleapis.com/maps/api/place/autocomplete/json?input={0}&key={1}", "Andheri West".Replace(" ", ","), "AIzaSyA5WiXTvjPSXEMGwFP0olr9vQuHSk-GVjY");
WebClient Webclient = new WebClient();
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
Uri featureUri = new Uri(url);
string responseString = Webclient.DownloadString(featureUri);
此外,请勿与我们分享您的密钥。
修改强>
显然,您的环境似乎需要代理配置,您可以在应用程序的配置文件中配置代理设置。它看起来像;
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy proxyaddress="proxyAddress" usesystemdefault="True"/>
</defaultProxy>
</system.net>