I am using HttpWebRequest to get the response back and save stream as json document on to my drive. When I test via Postman all works fine but when I run the below C# code I get error.
C#:
string url = "https://myapi.com/api/v1/city";
var result = "";
string json = string.Empty;
HttpWebRequest hwrequest = WebRequest.CreateHttp(url);
hwrequest.Method = "GET";
hwrequest.Accept = "application/json";
hwrequest.Credentials = new System.Net.NetworkCredential("user", "password");
using (var response = hwrequest.GetResponse())
using (var stream = new StreamReader(response.GetResponseStream()))
{
result = stream.ReadToEnd();
}
JavaScriptSerializer jss = new JavaScriptSerializer
{
MaxJsonLength = Int32.MaxValue
};
json = jss.Serialize(result);
using (StreamWriter sw = new StreamWriter(File.Create(@"C:\temp\city.json")))
{
sw.Write(json);
}
Error:
The underlying connection was closed. An unexpected error occurred on send.