I have HttpClient
code which is supposed to download a PDF/Word document from given URL (SSRS report) and convert it to a memory stream but that is not working for all cases.
Sample URL:
http://server/ReportServer_SQL1?/Team/Report&rs:Command=Render&user=employeename&Liability=Current&Role1=MGA&rs:Command=Render&rc:Parameters=Collapsed&rs:Format=image
Error:
System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) --- End of inner exception stack trace --- at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadData(Uri address)
Code
using (HttpClient client = new HttpClient())
{
client.Timeout = Timeout.InfiniteTimeSpan;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Basic " + encPassword);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
// Error happens on this line
var result = client.GetAsync(url).GetAwaiter().GetResult();
if (result.IsSuccessStatusCode)
{
var responseContent = result.Content;
var result_data = responseContent.ReadAsStreamAsync().GetAwaiter().GetResult();
ms = (MemoryStream)result_data;
}
else
{
throw new Exception("Authentication issue");
}
}