0

I'm using the PostAsync method of HTTPClient class. The url has changed and now it no longer works. I'm told that the request never gets through to the server and that I need to set the proxy settings. I can't find any proxy method on the HttpClient class, so what do I need to do?

HttpClient client = new HttpClient();
StringContent stringContent = new StringContent(msg, Encoding.UTF8, "application/json");
HttpResponseMessage resp = await client.PostAsync(url, stringContent);

The returned "resp" comes back with a 200 status and no error messages, but nothing is getting through. Any ideas?

I'm only a C# dabbler, so don't baffle me with too much C# tech talk, please.

Oh, and if it is of consequence, the new url is on AWS.

Note, this is code that used to work on the previous url. The only thing that has changed is the url and the proxy it is behind.

Graham
  • 7,807
  • 20
  • 69
  • 114
  • ...or just use new valid url? – Michał Turczyn Sep 23 '22 at 10:57
  • `Encoding.UTF8` <-- Avoid using `Encoding.UTF8` here because it will incorrectly render a BOM. Instead `new`-up your own private instance with `encoderShouldEmitUTF8Identifier: false`: https://learn.microsoft.com/en-us/dotnet/api/system.text.utf8encoding.-ctor?view=net-6.0#system-text-utf8encoding-ctor(system-boolean-system-boolean) – Dai Sep 23 '22 at 10:58
  • When you say "proxy" are you referring to a corporate-style SOCKS proxy (yay 1990s) - or do you mean a modern trendy web-application reverse-proxy like nginx? – Dai Sep 23 '22 at 10:59
  • I assume corporate-style SOCKS proxy – Graham Sep 23 '22 at 11:02
  • It's right here: `HttpClient.DefaultProxy` https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy?view=net-6.0 – Dai Sep 23 '22 at 11:11
  • I don't have the DefaultProxy option. Must be something that was added since. Am using .Net Framework 4.6.2. Have switched to 4.7.2. I can't go any higher than that as the "framework" that it is running in is at 4.7.2 – Graham Sep 23 '22 at 11:17
  • @Graham There is no reason to be targeting 4.6.2 today: every platform that ran 4.6.2 also supports 4.8.1 - _please_ update your project's target .NET versions as soon as you can - anyway, for .NET Framework 4.x, see here: https://stackoverflow.com/questions/16526689/using-a-proxy-with-net-4-5-httpclient – Dai Sep 23 '22 at 11:18
  • You can give a custom `HttpClientHandler` and set the `Proxy` property – Charlieface Sep 23 '22 at 11:33

0 Answers0