Https Request

Hello everyone,
I want to do a easy Https request.
I've searched a long time in the internet and found code like this:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
But this code isnt working...
I have crated an easy http request, which works very good:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                var encoding = Encoding.GetEncoding(response.CharacterSet);

                using (Stream responseStream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream, encoding))
                    return reader.ReadToEnd();
             }

But i need an Https Request. So how can I do that?
Thank you!

Best Answer

  • ChristopheBERNARDChristopheBERNARD BE ✭✭✭✭
    Accepted Answer

    i m sorry but not really time to explain here is it a sample who works and there are a lot of sample on google :)
    your goal is to get a json data ?
    `
    const string URL_PIZZA = "http://www.pizzaboy.de/app/pizzaboy.json";

    async Task GetAllPizzasDatas()
    {
    var httpClient = HttpClientApi.GetHttpClient();
    var response = await httpClient.GetAsync(URL_PIZZA);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                return content;
            }
    
            return null;
        }
    

    public class HttpClientApi
    {
    public static HttpClient GetHttpClient()
    {
    //
    var httpClient = new HttpClient();

            //
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
            return httpClient;
        }
    }
    

    `

    copy paste the code, and on click on button call yourclass.GetAllPizzasData()

Answers

Sign In or Register to comment.