Webrequest Webresponse 获取 403:禁止
Webrequest Webresponse getting 403: forbidden
我正在尝试获得更多有关 c# 的经验,因此我想在 Visual Studio 中使用 windows 表单制作一个小应用程序。该应用程序应从 https://launchlibrary.net 获取火箭发射时间并将其用于倒计时。
我没有使用 c# 从 Internet 获取数据的经验,所以我不知道我正在做的事情是否稍微正确。但这是我经过一番研究后得出的代码。
// Create a request for the URL.
WebRequest request = WebRequest.Create("https://launchlibrary.net/1.2/agency/5");
request.Method = "GET";
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromTheServer = reader.ReadToEnd();
// Display the content
MessageBox.Show(responseFromTheServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
问题在于行:
WebResponse response = request.GetResponse();
我收到以下错误
"The remote server returned an error (403) forbidden"
这是使用 WebClient 的示例,您必须设置 user-agent header,否则 403:
using (var webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
var response = webClient.DownloadString("https://launchlibrary.net/1.2/agency/5");
MessageBox.Show(response);
}
在 @user6522773 的帮助下,使用 WebRequest 从 GoogleApis 获取纬度和经度时遇到同样的问题,我将代码修改为以下内容:
using (var webClient = new WebClient())
{
string requestUri = $"http://maps.googleapis.com/maps/api/geocode/xml?address={Uri.EscapeDataString(address)}&sensor=false";
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
var response = webClient.DownloadString(requestUri);
XDocument xdoc = XDocument.Parse(response);
XElement result = xdoc.Element("GeocodeResponse").Element("result");
XElement locationElement = result.Element("geometry").Element("location");
XElement lat = locationElement.Element("lat");
XElement lng = locationElement.Element("lng");
MapPoint point = new MapPoint { Latitude = (double)lat, Longitude = (double)lng };
return point;
}
我正在尝试获得更多有关 c# 的经验,因此我想在 Visual Studio 中使用 windows 表单制作一个小应用程序。该应用程序应从 https://launchlibrary.net 获取火箭发射时间并将其用于倒计时。 我没有使用 c# 从 Internet 获取数据的经验,所以我不知道我正在做的事情是否稍微正确。但这是我经过一番研究后得出的代码。
// Create a request for the URL.
WebRequest request = WebRequest.Create("https://launchlibrary.net/1.2/agency/5");
request.Method = "GET";
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
MessageBox.Show(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromTheServer = reader.ReadToEnd();
// Display the content
MessageBox.Show(responseFromTheServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
问题在于行:
WebResponse response = request.GetResponse();
我收到以下错误
"The remote server returned an error (403) forbidden"
这是使用 WebClient 的示例,您必须设置 user-agent header,否则 403:
using (var webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
var response = webClient.DownloadString("https://launchlibrary.net/1.2/agency/5");
MessageBox.Show(response);
}
在 @user6522773 的帮助下,使用 WebRequest 从 GoogleApis 获取纬度和经度时遇到同样的问题,我将代码修改为以下内容:
using (var webClient = new WebClient())
{
string requestUri = $"http://maps.googleapis.com/maps/api/geocode/xml?address={Uri.EscapeDataString(address)}&sensor=false";
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
var response = webClient.DownloadString(requestUri);
XDocument xdoc = XDocument.Parse(response);
XElement result = xdoc.Element("GeocodeResponse").Element("result");
XElement locationElement = result.Element("geometry").Element("location");
XElement lat = locationElement.Element("lat");
XElement lng = locationElement.Element("lng");
MapPoint point = new MapPoint { Latitude = (double)lat, Longitude = (double)lng };
return point;
}