//Get Bytes using Web Request Method C#
//This is the method which is used for large data
private byte[] WebRequestOnHeaderByte(string url)
{
byte[] buffer = null;
int bytesProcessed = 0;
Stream remoteStream = null;
WebResponse response = null;
MemoryStream memorystream = null;
try
{
WebRequest request = WebRequest.Create(url);
if (request != null)
{
request.Proxy = new WebProxy(this.WebProxyUrl, true);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
response = request.GetResponse();
memorystream = new MemoryStream();
if (response != null)
{
remoteStream = response.GetResponseStream();
buffer = new byte[1024];
int bytesRead;
//This is loop which read 1KB bytes and add into memory stream and you can also add this stream into local file.
do
{
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
memorystream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
}
while (bytesRead > 0);
}
buffer = memorystream.ToArray();
}
}
catch (Exception ex)
{
Logger.Error("Error while getting bytes using web request", ex);
throw ex;
}
return buffer;
}
//This is the method which is used for large data
private byte[] WebRequestOnHeaderByte(string url)
{
byte[] buffer = null;
int bytesProcessed = 0;
Stream remoteStream = null;
WebResponse response = null;
MemoryStream memorystream = null;
try
{
WebRequest request = WebRequest.Create(url);
if (request != null)
{
request.Proxy = new WebProxy(this.WebProxyUrl, true);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
response = request.GetResponse();
memorystream = new MemoryStream();
if (response != null)
{
remoteStream = response.GetResponseStream();
buffer = new byte[1024];
int bytesRead;
//This is loop which read 1KB bytes and add into memory stream and you can also add this stream into local file.
do
{
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
memorystream.Write(buffer, 0, bytesRead);
bytesProcessed += bytesRead;
}
while (bytesRead > 0);
}
buffer = memorystream.ToArray();
}
}
catch (Exception ex)
{
Logger.Error("Error while getting bytes using web request", ex);
throw ex;
}
return buffer;
}
No comments:
Post a Comment