0

I have a WebClient that works fine without the proxy, however I need to utilize the proxy (requirement - so please understand it is required). The problem is when I use the proxy I am getting a 400 Bad Request Response.

Basic Code is as follows, any idea as to what could be wrong? Everything seems like I did things proper..

    WebClient webClient = new WebClient();

webClient.UseDefaultCredentials = true;
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 

AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.57");

    WebProxy wp = new WebProxy("http://mypacfile.mycompany.com/proxy.pac");
    
    webClient.Proxy = wp;
    webClient.ReadXml("https://www.InfoService.Info.xml");
    
    ... do stuff with response ...
Ken
  • 2,518
  • 2
  • 27
  • 35
  • 2
    There could be an error in the proxy. The proxy probably wants HTTPS (not HTTP). I've seen a lot of failure with proxies not handling the HTTP/HTTPS properly. The proxy is handling the HTTPS and then connecting to your URI with HTTP. – jdweng Jun 23 '23 at 14:20
  • @jdweng The proxy came straight from the InternetOptions settings on the machine - the web browser works fine and can access the xml feed – Ken Jun 23 '23 at 14:21
  • 1
    Have you tried with the equivalent to this while using HttpClient instead? – Fildor Jun 23 '23 at 14:30
  • 1
    This usually means an HTTP Header is wrong or missing. The default HTTP Headers are different in the browser and WebClient. One possibility is the UserAgent needs to be set which is the browser compatibility. See : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent – jdweng Jun 23 '23 at 14:35
  • @jdweng it works fine without the proxy , I have the header sand user agent set because the service will actively refuse the connection without it. – Ken Jun 23 '23 at 14:43
  • I'd probably spin up a tool for network inspection and trace what the differences are in the actual http packets on the wire. Then you have an example of what works and the diff to what doesn't. That gives you the chance to work your way to a working solution straight forward instead of poking around. – Fildor Jun 23 '23 at 14:56
  • You would usually be correct, but my environment does not permit me to see where that traffic is and what it looks like at each stage of the way.. – Ken Jun 23 '23 at 19:57

0 Answers0