Hi to everyone !!Specially to Mahesh :)
I want to login to a url and download pdf files
i used WebRequest.Credetials method but still unable to get the destination url
?????
Any help will be really appreciated
Loading
Hi to everyone !!Specially to Mahesh :)
I want to login to a url and download pdf files
i used WebRequest.Credetials method but still unable to get the destination url
?????
Any help will be really appreciated
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
rahul bhosale15 reputationPosted Jan 23, 2009, 5:03 AM EST
hi,
this may help u....
public
bool DownloadFile(string sourcePath, string destPath){
// Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try{
// Create a request for the specified remote file name WebRequest request = WebRequest.Create(FilePath); if (request != null){
// Send the request to the server and retrieve the // WebResponse objectresponse = request.GetResponse();
if (response != null){
// Once the WebResponse object has been retrieved, // get the stream object associated with the response's dataremoteStream = response.GetResponseStream();
// Create the local filelocalStream =
File.Create(Application.StartupPath + @"\" + destPath); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead;// Simple do/while loop to read from stream until
// no bytes are returned do{
//prgrssBarUpdatedVersionDownlaod.Value += 10; // Read data (up to 1k) from the streambytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local filelocalStream.Write(buffer, 0, bytesRead);
// Increment total bytes processedbytesProcessed += bytesRead;
}
while (bytesRead > 0);}
}
return true;}
catch (WebException ){
MessageBox.Show("Error"); return false;}
catch (Exception Ex){
MessageBox.Show(Ex.Message,
); return false;}
finally{
// Close the response and streams objects here // to make sure they're closed even if an exception // is thrown at some point if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close();}