using HTTP to download files from a secure (or unsecure) directory

G

Gina_Marano

Hey All,

I need to download 1 or more files from a secure or unsecure website
folder using HTTP.

Here is what I have so far:

public void GetHTTPImages()
{
WebClient HTTPClient = new WebClient();
HttpWebRequest webRequest;
HttpWebResponse webResponse;
Stream strResponse = null;
Stream strLocal = null;
bool isSuccessful = true;
string remoteFileName;
string localPath;
string serverAddress;
string uRL;
string destName;

GetFileToDownload(out remoteFileName,
out localPath,
out serverAddress);
try
{
while (remoteFileName != "")
{
uRL = serverAddress + remoteFileName;
destName = localPath+remoteFileName;

if (HTTPClient == null)
{
HTTPClient = new WebClient();
}

try
{
webRequest =
(HttpWebRequest)WebRequest.Create(uRL);
webRequest.Credentials =
CredentialCache.DefaultCredentials;
webResponse =
(HttpWebResponse)webRequest.GetResponse();
Int64 fileSize = webResponse.ContentLength;
if (fileSize == 0)
{
//do not want a 0 byte file, get another filename
GetFileToDownload(out remoteFileName,
out localPath,
out serverAddress);
continue;
}
}
catch
{
MessageBox.Show(ex.Message);
isSuccessful = false;
}

try
{
if (isSuccessful)
{
strResponse =
HTTPClient.OpenRead(uRL);
strLocal =
new FileStream(destName,
FileMode.Create,
FileAccess.Write,
FileShare.None);
int bytesSize = 0;
byte[] downBuffer = new byte[2048];

// Loop through the buffer until the buffer is empty
while ((bytesSize =
strResponse.Read(downBuffer, 0,
downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
isSuccessful = false;
}

if (isSuccessful)
{
HttpWebRequest HttpWRequest =
(HttpWebRequest)WebRequest.Create(uRL);
HttpWRequest.Method = "DELETE";
webResponse =
(HttpWebResponse)HttpWRequest.GetResponse();
webResponse.Close();
}
}
}
finally
{
if (strResponse != null)
strResponse.Close();
if (strLocal != null)
strLocal.Close();
}
}

1) How do I log in to a secure folder?
2) Can I keep the session open so I don't have to keep
connecting/logging in?
3) Any thoughts on my code above?

Thank you very much!

~Gina~
 
M

Miha Markic [MVP C#]

Hi Gina,

Gina_Marano said:
Hey All,

I need to download 1 or more files from a secure or unsecure website
folder using HTTP.

Here is what I have so far:

public void GetHTTPImages()
{
WebClient HTTPClient = new WebClient();
HttpWebRequest webRequest; ....


1) How do I log in to a secure folder?

What do you mean?
2) Can I keep the session open so I don't have to keep
connecting/logging in?

Create a CookieContainer instance and assign it to each HttpWebRequest.
3) Any thoughts on my code above?

Too much code to look :)
 
M

Miha Markic [MVP C#]

Hi Gina,

Gina_Marano said:
When I use the web browswer and go to the URL the IE default login
dialog appears.

Did you try setting HttpWebRequest.Credentials propery? (see help file)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top