neo

neo

  • NA
  • 2
  • 0

**URGENT** Download remote files in VB.Net using web request

Nov 4 2006 12:38 PM

Hi,

I am building a VB.NET application in which i need to automatically read a file's contents whose link exists on a web page for a third party web site.The link url for the file looks like this:

http://<DOMAIN>/ <...some path...> / 64ae64aabd27233f85256d3b0076549b/ a00421d13d0b0e1885256f04005c7511/ $FILE/ THE%20FILE.doc 

When i paste the url directly on the browser it gives a file download box (open/save).but i need to do the download through backend, using a code like this:

Dim wResp As HttpWebResponse
Dim sr As StreamReader
Dim txt As String
Dim streamWriter As System.IO.StreamWriter
Dim sw As StreamWriter
Dim wReq As HttpWebRequest
Dim exFolder As String

// strURL is the file url mentioned above

wReq = CType(WebRequest.Create(New Uri(strURL)), HttpWebRequest)

With wReq

.Proxy = WebRequest.DefaultWebProxy
.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
.Credentials =
New NetworkCredential(user, passwd, domain)
.CookieContainer = cookieJar
.UserAgent =
"BGClient"
'.UseBinary = True
.KeepAlive = True
.Headers.Set("Pragma", "no-cache")
'.Timeout = 1000000000
'.Method = System.Net.WebRequestMethods.Ftp.DownloadFile '"POST"
.ContentType = "application/msword"
'.ReadWriteTimeout = 1000000000
.PreAuthenticate = True

End With

wResp = wReq.GetResponse

sr =
New StreamReader(wResp.GetResponseStream)
txt = sr.ReadToEnd.Trim
sr.Close()
wResp.Close()
wReq =
Nothing

If Not txt Is Nothing And txt.Length > 0 Then

streamWriter = New StreamWriter(dFolder & strTitle & "." & fType, False)
streamWriter.WriteLine(txt)
streamWriter.Close()
streamWriter =
Nothing
fCount = fCount + 1

End
If

I have tried using web client and also FTPwebrequest, but the site does not accept ftp requests.
Please help.Thanks in advance!!