HtttpURLConnection nonexistent functions in Xamarin Forms development (Java to C # system conversion)

1

I have a system developed in Android Studio and I am passing to Xamarin Forms in C # and there are some errors that I can not solve

Summarizing what my system does: It takes the html page and transcribes line by line from the html so I can do some checks.

Here are the lines that I'm having trouble with:

if (httpURLConnection.getResponseCode () == 200) non-existent getResponseCode InputStream inputStream = new BufferedInputStream (httpURLConnection.getInputStream ()); getInputStream () does not exist

BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream, "UTF-8"));

    public string GetDados(string urlString)
    {
        string dados = string.Empty;

        try
        {
            URL url = new URL(urlString);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.OpenConnection();

            if(httpURLConnection.getResponseCode() == 200)
            {
                InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

                StringBuilder stringBuilder = new StringBuilder();

                string linha;

                while ((linha = bufferedReader.ReadLine()) != null)
                {
                    stringBuilder.Append(linha);
                }

                dados = stringBuilder.ToString();

                httpURLConnection.Disconnect();

            }
        }
        catch (IOException erro)
        {

            return null;
        }

        return dados;
    }
    
asked by anonymous 26.03.2017 / 03:06

1 answer

1

A much simpler way to download web content is this:

private async Task<string> DownloadContent(string url)
{
    using(var client = new HttpClient())
    {
        using(var r = await client.GetAsync(new Uri(url)))
        {
            return  await r.Content.ReadAsStringAsync();
        }
    }
}

You will need the HttpClient package.

    
17.04.2017 / 22:50