-1

I am calling localhost URL using C# HttWebRequest. It is returning a 503 service unavailable error in response.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://127.0.0.1:42000/some/path"));
req.Method = "POST";
byte[] postArray = Encoding.UTF8.GetBytes("");
req.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
req.ContentLength = postArray.Length;
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
response = (HttpWebResponse)req.GetResponse();

On a different computer it is working fine and also on web browser localhost URL is opening. Localhost is connected to AWS EC2 instance using tunnel on some port. What is cause of this issue?

phuzi
  • 12,078
  • 3
  • 26
  • 50
anujprashar
  • 6,263
  • 7
  • 52
  • 86
  • Is there a reason you're not using `HttpClient`? – Dai Nov 30 '21 at 10:07
  • "What is cause of this issue?" - **we can't tell you** - you haven't told us _any details_ of the webserver you're trying to connect to. – Dai Nov 30 '21 at 10:08
  • @Dai I have updated answer. – anujprashar Nov 30 '21 at 10:14
  • @Dai This is legacy code so cannot change to HttpClient. – anujprashar Nov 30 '21 at 10:15
  • @Dai Imagine if the asker had provided those details, then we'd know instead of having to guess! – Ian Kemp Nov 30 '21 at 10:52
  • @IanKemp I just thought of a very valid reason: `HttpClient` is `async`-only, and as we all know from (painful) personal experiences: async functions are viral - so I understand why a project might not want to deal with a month-long async-all-the-things marathon. – Dai Nov 30 '21 at 10:54

1 Answers1

0

I found out reason for it. Issue was due to HttpWebRequest was picking up system proxy automatically. So I set Proxy null in HttpWebRequest object in code req.Proxy = null; before making request. Issue was fixed after this.

anujprashar
  • 6,263
  • 7
  • 52
  • 86