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
"The underlying connection was closed: An unexpected error occurred on a send.".
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.
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();
I also tried to use another method (WebClient) to get the URL, but it didn't work as well.
String jsonResponse = null;
using (var webClient = new WebClient())
{
jsonResponse = webClient.DownloadString(url);
}
The full error I get on the exception:
The remote server returned an error: (401) Unauthorized. System at System.Net.HttpWebRequest.GetResponse()
Does anyone have any idea?
Thank you.