|
-
Jan 26th, 2025, 10:12 AM
#1
Thread Starter
Fanatic Member
webrequest with "wpad.dat" file for auto-configuration
Hello!
I wrote an application in VB.NET years ago which I have now re-written in VB6.
In that app I was using a downloader class (full code stated at the end) which used the following internally:
Code:
Dim webReq As HttpWebRequest
Dim webResp As HttpWebResponse
Try
webReq = CType(Net.WebRequest.Create(Me.Files(fileNr).URL), HttpWebRequest)
webReq.Proxy = New WebProxy("http://10.241.142.10:8080/wpad.dat")
webResp = CType(webReq.GetResponse, HttpWebResponse)
size = webResp.ContentLength
Catch ex As Exception
exc = ex
End Try
I have not found any examples on how to use such a "wpad.dat" file in other languages, and unfortunately I do not have access to such a wpad.dat file anymore to test it.
Could anybody tell me how to achieve the same in VB6?
Thank you very much for any help on this topic.
Code:
Option Strict On
Option Explicit On
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.IO
Imports System.Net
''' <summary>Class for downloading files in the background that supports info about their progress, the total progress, cancellation, pausing, and resuming. The downloads will run on a separate thread so you don't have to worry about multihreading yourself. </summary>
''' <remarks>Class FileDownloader v1.0.3, by De Dauw Jeroen - May 2009</remarks>
Public Class clsDownloader
Inherits System.Object
Implements IDisposable
#Region "Public Structure FileInfo"
'<summary>Simple structure for managing file info</summary>
Public Structure FileInfo
'<summary>The complete path of the file (directory + filename)</summary>
Public URL As String
'<summary>The name of the file</summary>
Public DownloadTo As String
'<summary>Create a new instance of FileInfo</summary>
'<param name="path">The complete path of the file (directory + filename)</param>
Public Sub New(ByVal uURL As String, ByVal uDownloadTo As String)
Me.URL = uURL
Me.DownloadTo = uDownloadTo
End Sub
End Structure
#End Region
#Region "Private Enum [Event]"
'<summary>Holder for events that are m_Triggered in the background worker but need to be fired in the main thread</summary>
Private Enum [Event]
CalculationFileSizesStarted
FileSizesCalculationComplete
DeletingFilesAfterCancel
FileDownloadAttempting
FileDownloadStarted
FileDownloadStopped
FileDownloadSucceeded
FileDownloadError
ProgressChanged
End Enum
#End Region
#Region "Private Enum InvokeType"
'<summary>Holder for the action that needs to be invoked</summary>
Private Enum InvokeType
EventRaiser
FileDownloadFailedRaiser
CalculatingFileNrRaiser
End Enum
#End Region
#Region "Events"
'<summary>Occurs when the file downloading has started</summary>
Public Event Started As EventHandler
'<summary>Occurs when the file downloading has been paused</summary>
Public Event Paused As EventHandler
'<summary>Occurs when the file downloading has been resumed</summary>
Public Event Resumed As EventHandler
'<summary>Occurs when the user has requested to cancel the downloads</summary>
Public Event CancelRequested As EventHandler
'<summary>Occurs when the user has requested to cancel the downloads and the cleanup of the downloaded files has started</summary>
Public Event DeletingFilesAfterCancel As EventHandler
'<summary>Occurs when the file downloading has been Cancelled by the user</summary>
Public Event Cancelled As EventHandler
'<summary>Occurs when the file downloading has been completed (without canceling it)</summary>
Public Event Completed As EventHandler
'<summary>Occurs when the file downloading has been stopped by either cancellation or completion</summary>
Public Event Stopped As EventHandler
'<summary>Occurs when the busy state of the FileDownloader has changed</summary>
Public Event IsBusyChanged As EventHandler
'<summary>Occurs when the pause state of the FileDownloader has changed</summary>
Public Event IsPausedChanged As EventHandler
'<summary>Occurs when the either the busy or pause state of the FileDownloader have changed</summary>
Public Event StateChanged As EventHandler
'<summary>Occurs when the calculation of the file sizes has started</summary>
Public Event CalculationFileSizesStarted As EventHandler
'<summary>Occurs when the calculation of the file sizes has started</summary>
Public Event CalculatingFileSize As FileSizeCalculationEventHandler
'<summary>Occurs when the calculation of the file sizes has been completed</summary>
Public Event FileSizesCalculationComplete As EventHandler
'<summary>Occurs when the FileDownloader attempts to get a web response to download the file</summary>
Public Event FileDownloadAttempting As EventHandler
'<summary>Occurs when a file download has started</summary>
Public Event FileDownloadStarted As EventHandler
'<summary>Occurs when a file download has stopped</summary>
Public Event FileDownloadStopped As EventHandler
'<summary>Occurs when a file download has been completed successfully</summary>
Public Event FileDownloadSucceeded As EventHandler
'<summary>Occurs when a file download has been completed unsuccessfully</summary>
Public Event FileDownloadFailed(ByVal sender As Object, ByVal e As Exception)
'<summary>Occurs every time a block of data has been downloaded</summary>
Public Event ProgressChanged As EventHandler
'Public Event FileDownloadError(ByVal uErrorDescription As String)
#End Region
#Region "Fields"
Public Delegate Sub FileSizeCalculationEventHandler(ByVal sender As Object, ByVal fileNumber As Int32)
Private WithEvents _BackgroundWorker As New BackgroundWorker
Private _Trigger As New Threading.ManualResetEvent(True)
' Preferences
Private _bSupportsProgress As Boolean
Private _bDeleteCompletedFiles As Boolean
Private _iPackageSize As Int32
Private _iStopWatchCycles As Int32
' State
Private _bDisposed As Boolean = False
Private _bBusy As Boolean
Private _bPaused As Boolean
Private _bCancelled As Boolean = True
Private _iCurrentFileProgress As Int64
Private _iTotalProgress As Int64
Private _iCurrentFileSize As Int64
Private _iCurrentSpeed As Int32
Private _iFileNr As Int32
' Data
Private _Files As New List(Of FileInfo)
Private _iTotalSize As Int64
#End Region
#Region "Constructors"
'<summary>Create a new instance of a FileDownloader</summary>
'<param name="supportsProgress">Optional. Boolean. Should the FileDownloader support total progress statistics?</param>
Public Sub New(Optional ByVal supportsProgress As Boolean = False)
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 https://www.vb-paradise.de/index.php/Thread/127427-HttpRequest-mit-HTTPS/
' Set the bgw properties
_BackgroundWorker.WorkerReportsProgress = True
_BackgroundWorker.WorkerSupportsCancellation = True
' Set the default class preferences
Me.SupportsProgress = supportsProgress
Me.PackageSize = 4096
Me.StopWatchCyclesAmount = 5
Me.DeleteCompletedFilesAfterCancel = False
End Sub
#End Region
Private _sWPadDatURL As String = String.Empty
Private _bSimulateDownloadSizeError As Boolean = False
Public Property SimulateDownloadSizeError() As Boolean
Get
Return _bSimulateDownloadSizeError
End Get
Set(value As Boolean)
_bSimulateDownloadSizeError = value
End Set
End Property
Public Property WPadDatUrl() As String
Get
Return _sWPadDatURL
End Get
Set(value As String)
_sWPadDatURL = value
End Set
End Property
#Region "Public methods"
'<summary>Start the downloads</summary>
Public Sub Start()
Me.IsBusy = True
End Sub
'<summary>pause the downloads</summary>
Public Sub Pause()
Me.IsPaused = True
End Sub
'<summary>Resume the downloads</summary>
Public Sub [Resume]()
Me.IsPaused = False
End Sub
'<summary>Stop the downloads</summary>
Public Overloads Sub [Stop]()
Me.IsBusy = False
End Sub
'<summary>Stop the downloads</summary>
'<param name="deleteCompletedFiles">Required. Boolean. Indicates wether the complete downloads should be deleted</param>
Public Overloads Sub [Stop](ByVal deleteCompletedFiles As Boolean)
Me.DeleteCompletedFilesAfterCancel = deleteCompletedFiles
Me.Stop()
End Sub
'<summary>Release the recources held by the FileDownloader</summary>
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
'<summary>Format an amount of bytes to a more readible notation with binary notation symbols</summary>
'<param name="size">Required. Int64. The raw amount of bytes</param>
'<param name="decimals">Optional. Int32. The amount of decimals you want to have displayed in the notation</param>
Public Shared Function FormatSizeBinary(ByVal size As Int64, Optional ByVal decimals As Int32 = 2) As String
' By De Dauw Jeroen - April 2009 - [email protected]
Dim sizes() As String = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
Dim formattedSize As Double = size
Dim sizeIndex As Int32 = 0
While formattedSize >= 1024 And sizeIndex < sizes.Length
formattedSize /= 1024
sizeIndex += 1
End While
Return Math.Round(formattedSize, decimals).ToString & sizes(sizeIndex)
End Function
'<summary>Format an amount of bytes to a more readible notation with decimal notation symbols</summary>
'<param name="size">Required. Int64. The raw amount of bytes</param>
'<param name="decimals">Optional. Int32. The amount of decimals you want to have displayed in the notation</param>
Public Shared Function FormatSizeDecimal(ByVal size As Int64, Optional ByVal decimals As Int32 = 2) As String
' By De Dauw Jeroen - April 2009 - [email protected]
Dim sizes() As String = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
Dim formattedSize As Double = size
Dim sizeIndex As Int32 = 0
While formattedSize >= 1000 And sizeIndex < sizes.Length
formattedSize /= 1000
sizeIndex += 1
End While
Return Math.Round(formattedSize, decimals).ToString & sizes(sizeIndex)
End Function
#End Region
#Region "Private/protected methods"
Private Sub _BackgroundWorker_DoWork() Handles _BackgroundWorker.DoWork
Dim iFileNr As Int32 = 0
If Me.SupportsProgress Then pCalculateFilesSize()
' If Not Directory.Exists(Me.LocalDirectory) Then Directory.CreateDirectory(Me.LocalDirectory)
While iFileNr < Me.Files.Count And Not _BackgroundWorker.CancellationPending
_iFileNr = iFileNr
pDownloadFile(iFileNr)
If _BackgroundWorker.CancellationPending Then
fireEventFromBgw([Event].DeletingFilesAfterCancel)
pCleanUpFiles(If(Me.DeleteCompletedFilesAfterCancel, 0, _iFileNr), If(Me.DeleteCompletedFilesAfterCancel, _iFileNr + 1, 1))
Else
iFileNr += 1
End If
End While
End Sub
Private Sub fireEventFromBgw(ByVal eventName As [Event])
_BackgroundWorker.ReportProgress(InvokeType.EventRaiser, eventName)
End Sub
Private Sub _BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles _BackgroundWorker.ProgressChanged
Select Case CType(e.ProgressPercentage, InvokeType)
Case InvokeType.EventRaiser
Select Case CType(e.UserState, [Event])
Case [Event].CalculationFileSizesStarted
RaiseEvent CalculationFileSizesStarted(Me, New EventArgs)
Case [Event].FileSizesCalculationComplete
RaiseEvent FileSizesCalculationComplete(Me, New EventArgs)
Case [Event].DeletingFilesAfterCancel
RaiseEvent DeletingFilesAfterCancel(Me, New EventArgs)
Case [Event].FileDownloadAttempting
RaiseEvent FileDownloadAttempting(Me, New EventArgs)
Case [Event].FileDownloadStarted
RaiseEvent FileDownloadStarted(Me, New EventArgs)
Case [Event].FileDownloadStopped
RaiseEvent FileDownloadStopped(Me, New EventArgs)
Case [Event].FileDownloadSucceeded
RaiseEvent FileDownloadSucceeded(Me, New EventArgs)
Case [Event].ProgressChanged
RaiseEvent ProgressChanged(Me, New EventArgs)
End Select
Case InvokeType.FileDownloadFailedRaiser
RaiseEvent FileDownloadFailed(Me, CType(e.UserState, Exception))
Case InvokeType.CalculatingFileNrRaiser
RaiseEvent CalculatingFileSize(Me, CInt(e.UserState))
End Select
End Sub
Private Sub pCleanUpFiles(Optional ByVal start As Int32 = 0, Optional ByVal length As Int32 = -1)
Dim last As Int32 = If(length < 0, Me.Files.Count - 1, start + length - 1)
For fileNr As Int32 = start To last
Dim fullPath As String = Me.Files(fileNr).DownloadTo
If IO.File.Exists(fullPath) Then IO.File.Delete(fullPath)
Next
End Sub
Private Sub pCalculateFilesSize()
fireEventFromBgw([Event].CalculationFileSizesStarted)
_iTotalSize = 0
For fileNr As Int32 = 0 To Me.Files.Count - 1
_BackgroundWorker.ReportProgress(InvokeType.CalculatingFileNrRaiser, fileNr + 1)
Try
Dim webReq As HttpWebRequest = CType(Net.WebRequest.Create(Me.Files(fileNr).URL), HttpWebRequest)
Dim webResp As HttpWebResponse = CType(webReq.GetResponse, HttpWebResponse)
_iTotalSize += webResp.ContentLength
webResp.Close()
Catch ex As Exception
_BackgroundWorker.ReportProgress(InvokeType.FileDownloadFailedRaiser, ex)
End Try
Next
fireEventFromBgw([Event].FileSizesCalculationComplete)
End Sub
Private Sub pDownloadFile(ByVal fileNr As Int32)
_iCurrentFileSize = 0
fireEventFromBgw([Event].FileDownloadAttempting)
Dim file As FileInfo = Me.Files(fileNr)
Dim size As Int64 = 0
Dim readBytes(Me.PackageSize - 1) As Byte
Dim currentPackageSize As Int32
Dim writer As FileStream
Try
writer = New FileStream(file.DownloadTo, IO.FileMode.Create)
Catch ex As Exception
Debug.Assert(False)
writer.Close()
_BackgroundWorker.ReportProgress(InvokeType.FileDownloadFailedRaiser, ex)
fireEventFromBgw([Event].FileDownloadError)
fireEventFromBgw([Event].FileDownloadStopped)
Return
'added by tom: RaiseEvent FileDownloadFailed(Nothing, ex)
End Try
Dim speedTimer As New Stopwatch
Dim readings As Int32 = 0
Dim exc As Exception = Nothing
Dim webReq As HttpWebRequest = Nothing
Dim webResp As HttpWebResponse = Nothing
Try
webReq = CType(Net.WebRequest.Create(Me.Files(fileNr).URL), HttpWebRequest)
webReq.Proxy = New WebProxy(_sWPadDatURL)
webResp = CType(webReq.GetResponse, HttpWebResponse)
size = webResp.ContentLength
Catch ex As Exception
exc = ex
End Try
_iCurrentFileSize = size
If exc IsNot Nothing Then
_BackgroundWorker.ReportProgress(InvokeType.FileDownloadFailedRaiser, exc)
fireEventFromBgw([Event].FileDownloadError)
Else
fireEventFromBgw([Event].FileDownloadStarted)
_iCurrentFileProgress = 0
While _iCurrentFileProgress < size
If _BackgroundWorker.CancellationPending Then
speedTimer.Stop()
writer.Close()
webResp.Close()
Exit Sub
End If
_Trigger.WaitOne()
speedTimer.Start()
currentPackageSize = webResp.GetResponseStream().Read(readBytes, 0, Me.PackageSize)
_iCurrentFileProgress += currentPackageSize
_iTotalProgress += currentPackageSize
fireEventFromBgw([Event].ProgressChanged)
writer.Write(readBytes, 0, currentPackageSize)
readings += 1
If readings >= Me.StopWatchCyclesAmount Then
_iCurrentSpeed = CInt(Me.PackageSize * StopWatchCyclesAmount * 1000 / (speedTimer.ElapsedMilliseconds + 1))
speedTimer.Reset()
readings = 0
End If
End While
speedTimer.Stop()
webResp.Close()
fireEventFromBgw([Event].FileDownloadSucceeded)
End If
If Not writer Is Nothing Then
writer.Close()
End If
fireEventFromBgw([Event].FileDownloadStopped)
End Sub
#End Region
End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|