Results 1 to 6 of 6

Thread: webrequest with "wpad.dat" file for auto-configuration

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Question 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

  2. #2
    Fanatic Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    629

    Re: webrequest with "wpad.dat" file for auto-configuration

    Have you attempted to get assistance from Copilot?

    This is the little blurb I got when I asked about it; maybe it at least gives you a direction to go?

    are you familiar with vb.net usage of httpWebRequest.Proxy while using a wpda.dat file?

    Yes, I can help with that! In VB.NET, you can use the HttpWebRequest.Proxy property to set proxy information for your web request. Here's a basic example of how you might do this:

    vb.net

    Code:
    Imports System.Net
    
    Module Module1
        Sub Main()
            Dim url As String = "http://www.example.com"
            Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
            Dim proxy As IWebProxy = request.Proxy
    
            If proxy IsNot Nothing Then
                Console.WriteLine("Proxy: " & proxy.GetProxy(request.RequestUri))
            Else
                Console.WriteLine("No proxy will be used.")
            End If
    
            ' Set a new proxy if needed
            Dim newProxy As New WebProxy("http://myproxy.example.com:8080")
            newProxy.Credentials = New NetworkCredential("username", "password")
            request.Proxy = newProxy
    
            ' Now you can proceed with your request
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            Console.WriteLine("Response received.")
        End Sub
    End Module

    This example demonstrates how to obtain the current proxy settings and set a new proxy. If you have a specific wpda.dat file you're working with, you might need to adjust the code to fit your particular use case.

    Does this help with what you're trying to achieve?


    are you aware of any way to do something similar with VB6?

    Yes, you can accomplish similar tasks in VB6 using the WinINet API to set up and manage proxy settings for your HTTP requests. Here is a simple example of how you can achieve this in VB6:

    First, you need to declare the necessary functions from the WinINet library:

    vb

    Code:
    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _
        ByVal lpszAgent As String, _
        ByVal dwAccessType As Long, _
        ByVal lpszProxy As String, _
        ByVal lpszProxyBypass As String, _
        ByVal dwFlags As Long _
    ) As Long
    
    Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" ( _
        ByVal hInternetSession As Long, _
        ByVal lpszUrl As String, _
        ByVal lpszHeaders As String, _
        ByVal dwHeadersLength As Long, _
        ByVal dwFlags As Long, _
        ByVal dwContext As Long _
    ) As Long
    
    Private Declare Function InternetReadFile Lib "wininet.dll" ( _
        ByVal hFile As Long, _
        ByVal lpBuffer As String, _
        ByVal dwNumberOfBytesToRead As Long, _
        lNumberOfBytesRead As Long _
    ) As Long
    
    Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _
        ByVal hInternet As Long _
    ) As Long
    Next, you can write a function to perform an HTTP request using a proxy:

    vb

    Code:
    Private Const INTERNET_OPEN_TYPE_PROXY = 3
    Private Const INTERNET_FLAG_RELOAD = &H80000000
    
    Public Sub GetHTTPData(ByVal url As String, ByVal proxy As String, ByVal username As String, ByVal password As String)
        Dim hInternetSession As Long
        Dim hUrl As Long
        Dim buffer As String
        Dim bytesRead As Long
        Dim result As String
    
        ' Initialize WinINet
        hInternetSession = InternetOpen("MyApp", INTERNET_OPEN_TYPE_PROXY, proxy, vbNullString, 0)
    
        If hInternetSession Then
            ' Open the URL
            hUrl = InternetOpenUrl(hInternetSession, url, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
    
            If hUrl Then
                buffer = Space(1024)
    
                ' Read the data
                Do
                    If InternetReadFile(hUrl, buffer, Len(buffer), bytesRead) = 0 Or bytesRead = 0 Then Exit Do
                    result = result & Left(buffer, bytesRead)
                Loop
    
                ' Close the URL handle
                InternetCloseHandle hUrl
            End If
    
            ' Close the Internet session handle
            InternetCloseHandle hInternetSession
        End If
    
        ' Display the result
        MsgBox result
    End Sub
    In this example, the GetHTTPData function sets up an Internet session with a proxy and retrieves data from the specified URL.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: webrequest with "wpad.dat" file for auto-configuration

    I think I am quite familiar with LLM, that is why I finally asked here...

    Or perhaps you want to defeat AI by feeding it back its own assumptions?
    :-)

  4. #4
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,538

    Re: webrequest with "wpad.dat" file for auto-configuration

    Set XmlHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    With XmlHttp
    '.SetProxy 2, "IP:Port"
    If InStr(GetUrl, "&__K") <> 0 Then .Option(6) = False '?????
    .Open "GET", GetUrl, True

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: webrequest with "wpad.dat" file for auto-configuration

    If you wanted to post a serious reply, then please show exactely how to do it, not something that seems to be copy pasted from your application...

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    760

    Re: webrequest with "wpad.dat" file for auto-configuration

    If somebody should have a solution, I would really appreciate seeing it.

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
  •  



Click Here to Expand Forum to Full Width