WebClient via EndPoint

StewartBW 745 Reputation points
2024-06-08T23:34:52.1333333+00:00

Hello

When using WebClient to send and/or receive data, how can I pass an EndPoint instance to the WebClient to connect via the specified IP and port?

https://video2.skills-academy.com/en-us/dotnet/api/system.net.endpoint?view=netframework-4.0

Possible?

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2024-06-10T05:45:55.9466667+00:00

    @StewartBW , Welcome to Microsoft Q&A,in .NET, WebClient doesn't directly support connecting via a specific IP address and port. If you need to connect to a specific IP address and port, you typically use other classes like HttpWebRequest.

    Here is a code example you could refer to:

        Sub Main()
            Dim request As HttpWebRequest = CType(WebRequest.Create("http://MyServer"), HttpWebRequest)
            request.ServicePoint.BindIPEndPointDelegate = New BindIPEndPoint(AddressOf BindIPEndPointCallback)
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        End Sub
        Public Function BindIPEndPointCallback(ByVal servicePoint As ServicePoint, ByVal remoteEndPoint As IPEndPoint, ByVal retryCount As Integer) As IPEndPoint
            Console.WriteLine("BindIPEndpoint called")
            Return New IPEndPoint(IPAddress.Any, 5000)
        End Function
    

    Best Regards,

    Jack


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.