C# WebRequest does not work with a particular link - c#

I am trying to ping a link using the code below and the it fails. It works fine in our development environment, but not in production. However, this a valid URL because I can copy and paste it in the browser and go to the site. Since this is in production, I cannot debug it locally on my machine. Can anyone think of what might be causing this? The issue is only with this link; other links work fine. The link I am trying is https://owl.english.purdue.edu/
System.Net.WebResponse objWebResponse = default(System.Net.WebResponse);
System.Net.WebRequest objWebRequest = System.Net.WebRequest.Create(URL);
objWebRequest.Timeout = TimeoutInMiliseconds;
try
{
PerformanceTimer.Start();
objWebResponse = objWebRequest.GetResponse();
PerformanceTimer.Finish();
try
{
this._ResponseTime = PerformanceTimer.ElapsedTime.Miliseconds;
}
catch (Exception ex)
{
string s = ex.ToString();
this._ResponseTime = -1;
}
{
this._ContentLength = objWebResponse.ContentLength;
this._ContentType = objWebResponse.ContentType;
int BytesRead = 0;
byte[] buffer = new byte[10001];
if (GetFileContent)
{
System.IO.Stream ReceiveStream = objWebResponse.GetResponseStream();
System.IO.MemoryStream MS = new System.IO.MemoryStream();
//memory stream is used so we can grab binary data
do
{
BytesRead = ReceiveStream.Read(buffer, 0, buffer.Length);
if (BytesRead <= 0) break;
MS.Write(buffer, 0, BytesRead);
}
while (true);
this._MemoryStream = MS;
ReceiveStream = null;
}
else
{
//This else is to validate the existence of the file, but not get their contents in case the file is big.
BytesRead = objWebResponse.GetResponseStream().Read(buffer, 0, buffer.Length);
if (BytesRead > 0)
{
this._MemoryStream = new System.IO.MemoryStream();
this._MemoryStream.Write(buffer, 0, BytesRead);
}
}
objWebResponse.Close();
}
functionReturnValue = true;
}
catch (System.Net.WebException ex)
{
this._Message = ex.Message;
}
catch (Exception ex2)
{
this._Message = ex2.Message;
}
objWebResponse = null;
objWebRequest = null;
PerformanceTimer = null;
return functionReturnValue;

Related

can't download from HttpWebRequest more than once

I am new to C# and trying to write a download manager with HttpWebRequest.
my manager calls a new class for breaking file into chunks for every file and that class calls a new downloader class.
my code for download is:
private void StartDownload()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
Console.WriteLine("request");
request.AddRange((int)_startPos, (int)_endPos);
WebResponse res = request.GetResponse();
Console.WriteLine("res: "+res);
RequestContentLength = 0;
try
{
bool isFinished = false;
isDownloading = true;
double speed = 0;
using (var responseStream = res.GetResponseStream())
{
using (FileStream localFileStream = new FileStream(FilePath, FileMode.Append))
{
Console.WriteLine("download started - "+ Url);
var buffer = new byte[4096];
int bytesRead;
DateTime lastDate = DateTime.Now;
int lastDownloaded = 0;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0 && isDownloading)
{
totalBytesRead += bytesRead;
if ((DateTime.Now - lastDate).TotalSeconds >= 1)
{
lastDate = DateTime.Now;
speed = totalBytesRead - lastDownloaded;
lastDownloaded = totalBytesRead;
}
RequestContentLength += bytesRead;
localFileStream.Write(buffer, 0, bytesRead);
double percentage = totalBytesRead * 100 / FileHeader.FileSize;
onDownloadProgress?.Invoke(ID, fname, percentage, totalBytesRead, FileHeader.FileSize, speed);
}
isFinished = true;
}
}
if (isFinished) {
onDownloadFinished?.Invoke(ID, fname, true, "success");
}
else
{
onDownloadFinished?.Invoke(ID, fname, false, "error");
}
isDownloading = false;
}
catch (Exception ex)
{
isDownloading = false;
onDownloadFinished?.Invoke(ID, fname, false, ex.Message);
}
}
the code works fine for the FIRST download ( I cropped the rest to make it shorter) but when I call it after first download I get Console.WriteLine("request"); to output but the rest stops and after a minute or two I get a timeout error at WebResponse res = request.GetResponse();
I couldn't name my exact problem and couldn't find a solution.
there are some similar posts about this and I tried most of them but doesn't work for me.

IHttpHandler over Https on IE

I'm looking for your expertise/help figuring out why my http handler works perfectly over http and doesn't over https (only IE problem, other browsers work fine either way). I'm using the handler to display images and when https is used on IE, it shows some images and fails to show others images.
again, the problem is only happening on https and IE, all other mutations with other browsers work just fine. below is my code:
public void ProcessRequest(HttpContext context)
{
Stream iStream = null;
try
{
if (!context.Response.IsClientConnected || HttpContext.Current == null || HttpContext.Current.Session["GoodUser"] == null)
return;
var url = context.Request.QueryString[#"ID"];
if (string.IsNullOrWhiteSpace(url))
return;
var fileUrl = EncryptionManager.DecryptUrl(url);
if (!File.Exists(fileUrl))
return;
var buffer = new byte[4096];
// Open the file.
iStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read, FileShare.Read);
// Total bytes to read:
var dataToRead = iStream.Length;
context.Response.AddHeader(#"Accept-Ranges", #"bytes");
context.Response.AddHeader(#"Content-Length", dataToRead.ToString());
context.Response.StatusCode= (int)HttpStatusCode.OK;
var file = new FileInfo(fileUrl);
context.Response.ContentType = MimeTypeMap.GetMimeType(file.Extension);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
context.Response.Cache.SetExpires(DateTime.MinValue);
if (!string.IsNullOrEmpty(context.Request.Headers[#"Range"]))
{
var range = context.Request.Headers[#"Range"].Split('=', '-');
var startbyte = int.Parse(range[1]);
iStream.Seek(startbyte, SeekOrigin.Begin);
context.Response.StatusCode = 206;
context.Response.AddHeader(#"Content-Range", $#" bytes {startbyte}-{dataToRead - 1}/{dataToRead}");
}
while (dataToRead > 0)
{
iStream.Read(buffer, 0, buffer.Length);
if (context.Response.IsClientConnected)
{
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.Flush();
buffer = new byte[buffer.Length];
dataToRead = dataToRead - buffer.Length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception exception)
{
}
finally
{
iStream?.Close();
context.ApplicationInstance.CompleteRequest();
}
}

Sending files from Android client to c# server, breaks before reading whole file

c# code
private MemoryStream Read() {
NetworkStream clientStream = _client.GetStream();
MemoryStream messageStream = new MemoryStream();
var inbuffer = new byte[65535];
if(clientStream.CanRead) {
do {
int bytesRead = clientStream.Read(inbuffer,0,inbuffer.Length);
messageStream.Write(inbuffer, 0, bytesRead);
messageStream.Flush();
Debug.Print(messageStream.Length + " added " + bytesRead);
} while(clientStream.DataAvailable); // GOES BEYOND HERE, EVEN THOUGH THERE'S MORE DATA
}
messageStream.Position = 0;
return messageStream;
}
android code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == ATTACH_FILE_REQUEST){
writeToSocket("<FILE>generic.jpeg");
InputStream input = null;
String filePath = data.getData().getPath().toString();
try {
input = getContentResolver().openInputStream(data.getData());
BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
byte[] bytes = new byte[65535];
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
while((bufferedInputStream.read(bytes)) != -1){
byteArray.write(bytes, 0, bytes.length);
}
socketNetworkHelper.writeFile(byteArray.toByteArray());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Toast.makeText(this, "Error in file selection", Toast.LENGTH_LONG).show();
} catch (IOException e2){
e2.printStackTrace();
Toast.makeText(this, "Input/Output Error", Toast.LENGTH_LONG).show();
}
}
}
}
In SocketNetworkHelper.java which has a socket connected:
public void writeFile(byte[] buffer) {
try {
socket.getOutputStream().write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The question is: How can I get the whole file?
You could also send the filesize as a parameter
private MemoryStream Read(int filesize) {
NetworkStream clientStream = _client.GetStream();
MemoryStream messageStream = new MemoryStream();
var inbuffer = new byte[filesize];
if (clientStream.CanRead) {
int totalBytesRead = 0;
do {
int bytesRead = clientStream.Read(inbuffer, 0, inbuffer.Length);
totalBytesRead += bytesRead;
messageStream.Write(inbuffer, 0, bytesRead);
messageStream.Flush();
} while (totalBytesRead < filesize);
}
messageStream.Position = 0;
return messageStream;

Error on making resumeable download in c#

I was trying to test a resumable download using c#. I found that add range will help by looking at some blogs. But in the following code add range have no meaning.
Guys Suggest me how to solve the issue.
What method will be effective to make a Resumable download??
HttpWebRequest myrequest = null;
HttpWebResponse myresponse = null;
private int interval=2048;
public bool set_url(string todonwloads,string tosaves)
{
this.todownload = todonwloads;
this.tosave = tosaves;
return true;
}
public bool start_download()
{
myrequest = (HttpWebRequest)WebRequest.Create(this.todownload);
// it the following code.
//If i dont write addrange. It will download same portion of the file.
myrequest.AddRange(4000,8000);
try
{
myresponse = (HttpWebResponse)myrequest.GetResponse();
if (myresponse.StatusCode == HttpStatusCode.OK)
{
Stream ReceiveSteam = myresponse.GetResponseStream();
FileStream fs = new FileStream(
this.tosave,
FileMode.Create,
FileAccess.Write,
FileShare.None);
int reads;
byte[] buffer = new byte[this.interval];
while ((reads = ReceiveSteam.Read(
buffer,
0,
this.interval)) > 0)
{
fs.Write(buffer, 0, reads);
}
return true;
}
}
catch (WebException ex)
{
throw ex;
}
finally
{
if (myresponse != null)
{
myresponse.Close();
}
}
return false;
}
You currently create and overwrite the file every time you download a section of the file:
// Overwrites the file each time -\/
... = new FileStream(this.tosave, FileMode.Create, ...
You instead need to open or create the file using FileMode.OpenOrCreate, then seek to the last section of the file you wrote to:
// seek to the last end offset, you'll need to save this somehow
fs.Seek(lastOffset, SeekOrigin.Begin);
int reads;
byte[] buffer = new byte[this.interval];
while ((reads = ReceiveSteam.Read(buffer, 0, this.interval)) > 0)
{
fs.Write(buffer, 0, reads);
lastOffset += reads;
}

IOEXCEPTION in C# compact framework when saving image

I'm saving an image from a web request and something really weird is happening. On roughly half of the 8,000 images I'm downloading I get IOEXCEPTION errors:
ERROR_ACCESS_DENIED (5)
INVALID_PARAMETER (87)
Before I save the file using file.open, I check to make sure the file does not exist. The exception is thrown at this line of code:
fileStream = File.Open(destination, FileMode.Create, FileAccess.Write, FileShare.None);
Below is the code:
public static bool DownloadFile(string url, string destination)
{
bool success = false;
System.Net.HttpWebRequest request = null;
System.Net.WebResponse response = null;
Stream responseStream = null;
FileStream fileStream = null;
try
{
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 100000; // 100 seconds
request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
response = request.GetResponse();
responseStream = response.GetResponseStream();
fileStream = File.Open(destination, FileMode.Create, FileAccess.Write, FileShare.None);
//fileStream = File.Create(destination);
// read up to ten kilobytes at a time
int maxRead = 10240;
byte[] buffer = new byte[maxRead];
int bytesRead = 0;
int totalBytesRead = 0;
// loop until no data is returned
while ((bytesRead = responseStream.Read(buffer, 0, maxRead)) > 0)
{
totalBytesRead += bytesRead;
fileStream.Write(buffer, 0, bytesRead);
}
// we got to this point with no exception. Ok.
success = true;
}
catch (System.Net.WebException we)
{
// something went terribly wrong.
success = false;
//MessageBox.Show(exp.ToString());
writeErrFile(we.ToString(), url);
//Debug.WriteLine(exp);
}
catch (System.IO.IOException ie)
{
// something went terribly wrong.
success = false;
//MessageBox.Show(ie.InnerException.ToString());
writeErrFile(ie.ToString(), destination + " -- " + url);
//Debug.WriteLine(exp);
}
catch (Exception exp)
{
// something went terribly wrong.
success = false;
//MessageBox.Show(exp.ToString());
writeErrFile(exp.ToString(), destination + " -- " + url);
//Debug.WriteLine(exp);
}
finally
{
// cleanup all potentially open streams.
if (null != responseStream)
responseStream.Close();
if (null != response)
response.Close();
if (null != fileStream)
fileStream.Close();
}
// if part of the file was written and the transfer failed, delete the partial file
if (!success && File.Exists(destination))
File.Delete(destination);
return success;
}
I've been stuck on this for a couple of days. Any help would be appreciated in unimaginable orders of magnitude.
Use file.exists() to check if the file exists and file.create or file.openwrite to write the file.
From your code I can't see how you are checking the file exists.

Categories