Xamarin:无法从站点获取数据
Xamarin: Сan't get data from the site
我正在尝试从站点获取信息。为此,我将整个页面请求为一个字符串,然后只查找必要的信息。
但不幸的是,我无法从我需要的网站上做到这一点。在移动应用程序中,我可以从 Yandex、Google 和其他网站获取信息,但不能从我自己的网站获取信息。该程序只是在 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
行冻结,几分钟后我收到错误消息“操作已超时”
但与此同时,当我在常规应用程序(不是 Xamarin)中使用相同的代码时,我可以毫无问题地从我的站点获取信息。
我尝试了不同的解析器 - 同样的事情。
代码:
private async void Button_Clicked(object sender, EventArgs e)
{
//string url = "https://yandex.ru";
string url = "https://lhl-77.ru";
string data = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//var responseStream = response.GetResponseStream();
//var sr = new StreamReader(responseStream);
//var responseHtml = sr.ReadToEnd();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
int found = data.IndexOf("<title>");
int found2 = data.IndexOf("</title>");
text.Text = "2";
qwe.Text = data.Substring(found + 7, 10);
qwe2.Text = "3";
}
}
}
我修复了 Internet 连接,之后我收到证书错误,解决方法如下:
request.ServerCertificateValidationCallback = delegate { return true; };
我正在尝试从站点获取信息。为此,我将整个页面请求为一个字符串,然后只查找必要的信息。
但不幸的是,我无法从我需要的网站上做到这一点。在移动应用程序中,我可以从 Yandex、Google 和其他网站获取信息,但不能从我自己的网站获取信息。该程序只是在 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
行冻结,几分钟后我收到错误消息“操作已超时”
但与此同时,当我在常规应用程序(不是 Xamarin)中使用相同的代码时,我可以毫无问题地从我的站点获取信息。
我尝试了不同的解析器 - 同样的事情。
代码:
private async void Button_Clicked(object sender, EventArgs e)
{
//string url = "https://yandex.ru";
string url = "https://lhl-77.ru";
string data = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
//var responseStream = response.GetResponseStream();
//var sr = new StreamReader(responseStream);
//var responseHtml = sr.ReadToEnd();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
int found = data.IndexOf("<title>");
int found2 = data.IndexOf("</title>");
text.Text = "2";
qwe.Text = data.Substring(found + 7, 10);
qwe2.Text = "3";
}
}
}
我修复了 Internet 连接,之后我收到证书错误,解决方法如下:
request.ServerCertificateValidationCallback = delegate { return true; };