2

I'm trying to get a web request working using HttpClient. It works when using http, but does not work with https. The URL I'm testing with works in a browser with both http and https, however, when using the code below it works only with http.

One of the URLs I test with is one I setup with both http and https. This code has the same problem there as well.

Do I need to add another confuration to make HttpClient work with https?

Note: I have used the proxy below in a Python app and it works for both http and https so the problem is not the proxy.

public static async System.Threading.Tasks.Task Main(string[] args) { var url = "https://www.google.com"; using (System.Net.Http.HttpClientHandler handler = new System.Net.Http.HttpClientHandler() { Proxy = new System.Net.WebProxy(@"http://38.39.202.55:80"), UseProxy = true, }) { using (System.Net.Http.HttpClient hc = new System.Net.Http.HttpClient(handler)) { hc.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html)"); string data = ""; using (System.Net.Http.HttpResponseMessage response = await hc.GetAsync(url)) { data = await response.Content.ReadAsStringAsync(); System.Console.WriteLine(data); } System.Console.WriteLine(data); } } } 

Thank You

3
  • "The URL I'm testing with works in a browser with both http and https" - is that too with a proxy? Commented May 6, 2021 at 23:17
  • I think there are is a cookie that is causing confusion. What you posted is not consistent. So I recommend repeating your test and between each test go into a browser and manually delete all cookies. Commented May 7, 2021 at 0:00
  • OK my browser comment was confusing. What I meant by that is some websites accept only http or https. the browser test was to verify that the website runs on both meaning that the c# code above should work. Commented May 7, 2021 at 4:13

1 Answer 1

1

It may have to do with the verification of ssl certificates. Thus, for example, you could edit the web.config file to bypass ssl errors. Like this:

 <system.net> <settings> <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" /> </settings> </system.net> 
Sign up to request clarification or add additional context in comments.

2 Comments

You could also write methods that does the same thing. :)
This is a console app so I added the config data into the app.config directly below <configuration> and it did not work. Same result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.