title: HttpWebRequest doesn't work, but Postman does
body: <p>I'm trying to get a json string via WebHttpRequest using the code below (it tries to call a file that will return the json for me which is in the application). It worked on my machine but when I deployed to the user server, I got the error </p>
  
  <blockquote>
    <p>"The underlying connection was closed: An unexpected error occurred on
    a send.".</p>
  </blockquote>
  
  <p>I got the same error when I try to run the powershell invoke-webrequest for the URL, but weirdly it works fine when I use Postman (get or post) for the URL, inside or outside the user server.</p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  request.UseDefaultCredentials = true;
  request.PreAuthenticate = true;
  request.Credentials = CredentialCache.DefaultCredentials;
  request.ContentType = "application/json";
  
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  Stream resStream = response.GetResponseStream();
  StreamReader reader = new StreamReader(resStream);
  String jsonResponse = reader.ReadToEnd();
  </code></pre>
  
  <p>I also tried to use another method (WebClient) to get the URL, but it didn't work as well.</p>
  
  <pre><code>String jsonResponse = null;
  using (var webClient = new WebClient())
  {
      jsonResponse = webClient.DownloadString(url);
  }
  </code></pre>
  
  <p>The full error I get on the exception:</p>
  
  <blockquote>
    <p>The remote server returned an error: (401) Unauthorized.
    System
       at System.Net.HttpWebRequest.GetResponse()</p>
  </blockquote>
  
  <p>Does anyone have any idea?</p>
  
  <p>Thank you.</p>