!?!@?$?!@$?!@? Finally!!! It was so much work, I had to share!
After hours and hours (literally 7 hours) of searching testing I finally was able to open a file transfer silently in the background, in which the link was pulled from a webbrowser which has already logged in with HTTPS using cookies and then I transfered the cookie over to the HttpWebRequest.
I used this to open XML and XLS files to read data from.
Talk about a Pain! (ITA)
Start with this:
Code:Const INTERNET_COOKIE_HTTPONLY As Integer = &H2000 Declare Function InternetGetCookieEx Lib "wininet.dll" Alias "InternetGetCookieExA" (ByVal pchURL As String, ByVal pchCookieName As String, _ ByVal pchCookieData As System.Text.StringBuilder, ByRef pcchCookieData As Integer, ByVal flags As Integer, ByVal reserved As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean Public Function GetGlobalCookies(ByVal uri As String) As String Dim datasize As UInteger = 8192 Dim cookieData As New StringBuilder(CInt(datasize)) 'MsgBox(InternetGetCookieEx(uri, Nothing, cookieData, datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)) If InternetGetCookieEx(uri, Nothing, cookieData, datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero) AndAlso cookieData.Length > 0 Then Return cookieData.ToString().Replace(";"c, ","c) Else Return Nothing End If End Function
Then your sub to call the download of your "link"
Code:Private Sub DownloadFile(ByVal remoteFilename As Uri, ByVal localFilename As String) Dim webRequest As HttpWebRequest = Net.WebRequest.Create(remoteFilename) Dim bytesProcessed As Integer = 0 Dim remoteStream As Stream Dim localStream As Stream Dim response As WebResponse webRequest.CookieContainer = New CookieContainer webRequest.CookieContainer.SetCookies(WebBrowser2.Document.Url, GetGlobalCookies(WebBrowser1.Document.Url.AbsoluteUri)) response = webRequest.GetResponse() If Not response Is Nothing Then remoteStream = response.GetResponseStream() localStream = File.Create(localFilename) 'Declare buffer as byte array Dim myBuffer As Byte() 'Byte array initialization ReDim myBuffer(1024) Dim bytesRead As Integer bytesRead = remoteStream.Read(myBuffer, 0, 1024) Do While (bytesRead > 0) localStream.Write(myBuffer, 0, bytesRead) bytesProcessed += bytesRead bytesRead = remoteStream.Read(myBuffer, 0, 1024) Loop localStream.Close() End If End Sub
...and then to call the URL from the HTML page...
Code:Dim anchors As HtmlElementCollection = WebBrowser2.Document.GetElementsByTagName("a") For Each anc As HtmlElement In anchors If anc.InnerText = frmMain.ListBox1.SelectedItem Then 'Call the download, setting the URL and the local path to save the file. DownloadFile(New Uri(anc.GetAttribute("href")), "c:\temp\filename.xxx") End If
I even delete the file when I exit, and I'll share that with you guys as well.
Code:Private Sub DeletePath() Dim KillFile As String KillFile = path2 'Path 2 = a constant path thoughout program 'Check that file exists If Len(Dir$(KillFile)) > 0 Then 'First remove readonly attribute, if set SetAttr(KillFile, vbNormal) 'Then delete the file Kill(KillFile) End If End Sub




Reply With Quote