Home > OS >  Xamarin: Сan't get data from the site
Xamarin: Сan't get data from the site

Time:01-28

I'm trying to get information from the site. To do this, I request the entire page as a string, and then just look for the necessary info. But unfortunately, I can't do it from the site I need. In the mobile app, I can get information from Yandex, Google and other sites, but I can't from my own. The program just freezes on the line using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) and in a few minutes I get the error "The operation has timed out"

But at the same time, when I use the same code in a regular application (not Xamarin), I get information from my site without problems.

I tried different parsers - the same thing.

Code:

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";
                }
            }
        } 

CodePudding user response:

I fixed the Internet connection, after that I got an error with the certificate, which was solved like this:

 request.ServerCertificateValidationCallback = delegate { return true; };
  •  Tags:  
  • Related