由于 'WebRequest' 导致的 GetResponse() 错误
GetResponse() error due to 'WebRequest'
我正在尝试向 Google API 提出请求。但是我收到 GetResponse()
的错误。我得到的错误是...
'WebRequest' does not contain a definition for 'GetResponse'...
Visual Studio 让我可以选择用 BeginGetResponse()
替换它,但我不确定如何格式化代码或更改我已经拥有的代码来完成此操作。谁能指出我正确的方向来解决这个问题?有可能我错过了一些程序集...但我不认为我是。
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
var address = addressInput.Text;
var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=false", Uri.EscapeDataString(address));
MessageBox.Show(requestUri.ToString());
var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = XDocument.Load(response.GetResponseStream());
var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var lat = locationElement.Element("lat");
var lng = locationElement.Element("lng");
}
您可以像这样使用异步版本:
var request = WebRequest.Create(requestUri);
request.BeginGetResponse(this.FinishWebRequest, request);
然后 FinishWebRequest
方法将在远程服务器发送响应时调用:
private void FinishWebRequest(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
using (var response = request.EndGetResponse(result))
using (var responseStream = response.GetResponseStream())
{
var xdoc = XDocument.Load(responseStream);
...
}
}
或者如果您更喜欢使用匿名方法:
var request = WebRequest.Create(requestUri);
request.BeginGetResponse(result =>
{
using (var response = request.EndGetResponse(result))
using (var responseStream = response.GetResponseStream())
{
var xdoc = XDocument.Load(responseStream);
...
}
}, null);
我正在尝试向 Google API 提出请求。但是我收到 GetResponse()
的错误。我得到的错误是...
'WebRequest' does not contain a definition for 'GetResponse'...
Visual Studio 让我可以选择用 BeginGetResponse()
替换它,但我不确定如何格式化代码或更改我已经拥有的代码来完成此操作。谁能指出我正确的方向来解决这个问题?有可能我错过了一些程序集...但我不认为我是。
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
var address = addressInput.Text;
var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=false", Uri.EscapeDataString(address));
MessageBox.Show(requestUri.ToString());
var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = XDocument.Load(response.GetResponseStream());
var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var lat = locationElement.Element("lat");
var lng = locationElement.Element("lng");
}
您可以像这样使用异步版本:
var request = WebRequest.Create(requestUri);
request.BeginGetResponse(this.FinishWebRequest, request);
然后 FinishWebRequest
方法将在远程服务器发送响应时调用:
private void FinishWebRequest(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
using (var response = request.EndGetResponse(result))
using (var responseStream = response.GetResponseStream())
{
var xdoc = XDocument.Load(responseStream);
...
}
}
或者如果您更喜欢使用匿名方法:
var request = WebRequest.Create(requestUri);
request.BeginGetResponse(result =>
{
using (var response = request.EndGetResponse(result))
using (var responseStream = response.GetResponseStream())
{
var xdoc = XDocument.Load(responseStream);
...
}
}, null);