By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

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

P: n/a
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~

Oct 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Gina,

"Gina_Marano" <gi*******@gmail.comwrote in message
news:11**********************@e64g2000cwd.googlegr oups.com...
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 :-)

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Oct 29 '06 #2

P: n/a

Miha Markic [MVP C#] wrote:
1) How do I log in to a secure folder?

What do you mean?
When I use the web browswer and go to the URL the IE default login
dialog appears.
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.
thanks much Miha, I will look into the CookieContainer.
3) Any thoughts on my code above?

Too much code to look :-)
~Gina~

Oct 30 '06 #3

P: n/a
Hi Gina,

"Gina_Marano" <gi*******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
Miha Markic [MVP C#] wrote:
1) How do I log in to a secure folder?

What do you mean?

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)

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Oct 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.