venkat

venkat

  • NA
  • 2
  • 0

Problem downloadin file using HttpWebRequest from a WinApp

Oct 3 2007 2:19 AM


Hi folks,

Please help....

Im trying to download a file from webserver to my win app using webrequest and webresponse classes. Its working fine on local machines.  But Im not able to download files from remote server. I colud not locate where is it going wrong.

FYI..... code snippets...

1. Download.aspx.cs

strGetFileList = CType(Request.QueryString("GetFileList"), String)

strFolderPath = ConfigurationManager.AppSettings.Get("DownloadPath")

If (strGetFileList = "Yes") Then

'If yes, first it will supply all the files available in the directory

strFileList = GetFileNamesList(strFolderPath)

Response.ClearHeaders() 'clearing the headers(if present) from the file

Response.AddHeader("FileList", strFileList) 'put the file name in the addheader property of the "response"

ElseIf (strGetFileList = "No") Then

'If NO, means it will ask for a particular file one by one

strFileName = CType(Request.QueryString("FileName"), String)

oFileStream = New FileStream(strFolderPath & strFileName, FileMode.Open)

'clearing the headers(if present) from the file

Response.ClearHeaders()

'put the file name in the addheader property of the "response"

Response.AddHeader("FileName", strFileName)

'Response.ContentType = "application/octet-stream"

''copy the data to the output stream

CopyData(oFileStream, Response.OutputStream)

Response.Write(Response.OutputStream)

End If

Catch ex As Exception

ExceptionManager.Publish(ex)

Finally

Response.OutputStream.Close()

If Not oFileStream Is Nothing Then oFileStream.Close()

'end the response

Response.End()

End Try

 

2. Download.vb ( from my Winapp)

strQueryString = "Download.aspx?GetFileList=Yes"

strSourceURL = fstrSourceURL & strQueryString

Try

'Get the list of file names as , separated values in the web server to be downloaded

'then download each file by passing the file name

' This sets up the Request instance

oRequest = WebRequest.Create(strSourceURL)

oRequest.Timeout = 600000

' getting the default proxy settings for solving the bad data error during data sync

oProxy = New WebProxy()

oProxy = WebProxy.GetDefaultProxy

If Not IsNothing(oProxy.Address) Then

' ' getting the default user id, password and domain name

' ' for user authentication.

 oProxy.Credentials = CredentialCache.DefaultCredentials

 oRequest.Proxy = oProxy

End If

oRequest.Method = "GET" ' Use a GET since no data is being sent to the web server

oResponse = oRequest.GetResponse ' This causes the round-trip

'Get the file names of the file that is being downloaded as , separated values

strFileList = oResponse.Headers("FileList")

If (strFileList.Length > 0) And (Not strFileList = Nothing) Then

'Remove the , from the end

strFileList = strFileList.Substring(0, (strFileList.Length - 1))

arrFileList = strFileList.Split(",".ToCharArray())

Else

Throw New Exception("No file found to download")

End If

 

'Loop through the files and downloadeach file from the destination folder

For i = 0 To arrFileList.Length - 1

oRequest = Nothing

oResponse = Nothing

strQueryString = "Download.aspx?GetFileList=No"

strSourceURL = fstrSourceURL & strQueryString & "&FileName=" & arrFileList(i)

' This sets up the Request instance

oRequest = CType(WebRequest.Create(strSourceURL), HttpWebRequest)

' Use a GET since no data is being sent to the web server

oRequest.Method = "GET"

' This causes the round-trip

oResponse = oRequest.GetResponse

'Get the file name of the file that is being downloaded.

strFileName = oResponse.Headers("FileName")

'check if the file name is empty,indicates that there is no file on Portal to download

If Not strFileName = Nothing Then

strDestURL = fstrDestURL & strFileName

'Check whether file exists, if exists delete the old file

oFileInfo = New FileInfo(strDestURL)

If (oFileInfo.Exists()) Then

oFileInfo.Delete()

End If

' Open the file to stream in the content

oFileStream = New FileStream(strDestURL, FileMode.Create)

' Copy the content from the response stream to the file.

CopyData(oResponse.GetResponseStream(), oFileStream)

m_strFileName = strFileName

If Not oResponse Is Nothing Then oResponse.GetResponseStream.Close()

If Not oFileStream Is Nothing Then oFileStream.Close()

Else

Throw New Exception("File not found.")

End If

Next i

bRetValue = True

Return bRetValue

Catch ex As Exception

ExceptionManager.Publish(ex)

Throw ex

Finally

If Not oResponse Is Nothing Then oResponse.GetResponseStream.Close()

If Not oFileStream Is Nothing Then oFileStream.Close()

oRequest = Nothing

oResponse = Nothing

oFileInfo = Nothing

End Try

--------------------------------------------------------------------------------------------------------------

should i need to configure anything, please suggest some way to work it out

Regards,

venkat