(vb.net) How do I make a connection through a proxy.

Sylonious

Active member
Joined
Apr 12, 2003
Messages
27
Private ns As NetworkStream
Private client As New TcpClient()

Public Function Open()
client.Connect(_server, _port)
ns = client.GetStream()
Dim strMessage As String = "Message" & ControlChars.CrLf
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(strMessage)
ns.Write(sendBytes, 0, sendBytes.Length)
End Function

I thought it was like this.
Public Function open()
Dim Proxy As New System.Net.WebProxy("http://" & _proxy & "/")
Dim strmessage2 As String = "CONNECT " & _server & ":" & "_port" & HTTP/1.0"
Dim Sendbytes2 As [Byte]() = Encoding.ASCII.GetBytes(strmessage2, 0, strmessage2.Length)
Return Sendbytes2
End Function
 
If your site uses a proxy to provide access to the Internet, you must configure a proxy instance to enable your application to communicate with the Web proxy.

The following code example creates a global proxy instance that will enable any WebRequest to use a proxy to communicate with the Internet. The example assumes that the proxy server is named webproxy and that it communicates on port 80, the standard HTTP port.

[C#]
WebProxy proxyObject = new WebProxy("http://webproxy:80/");
GlobalProxySelection.Select = proxyObject;
[Visual Basic]
Dim proxyObject As WebProxy = New WebProxy("http://webproxy:80/")
GlobalProxySelection.Select = proxyObject
You can override the global proxy selection by assigning an instance that implements the IWebProxy interface to the Proxy property of your WebRequest. The following code example sends a WebRequest to www.contoso.com that overrides the global proxy selection with a proxy server named alternateproxy on port 80.

[C#]
WebRequest req = new WebRequest.Create("http://www.contoso.com/");
req.Proxy = new WebProxy("http://alternateproxy:80/");
[Visual Basic]
Dim req As WebRequest = new WebRequest.Create("http://www.contoso.com/")
req.Proxy = New WebProxy("http://alternateproxy:80/")
 
Actually, that is an old post. I finished that part of the program I needed to make a socket connection to a proxy and then to a host.
 
ok

Code:
Proxye = Proxy.Text.ToString.Split(":")
                        
                        Dim Proxyz As String = System.Net.Dns.GetHostByAddress(Proxye(0)).HostName.ToString
                        
                        Client.Connect(Proxye(0), Proxye(1))
                        Dim ns As NetworkStream = Client.GetStream()
                         Do a simple write.
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("CONNECT " & Host.Text & ":" & port & " HTTP/1.1" & ControlChars.CrLf & "Host: " & host.Text & ":" & port & ControlChars.CrLf & ControlChars.CrLf)
                        ns.Write(sendBytes, 0, sendBytes.Length)

Simple. If you want to use a proxy for webpages you have to send a little more info.
 
My program does not do that. I would have to look up the RFC commands to find it. Its not like I am a guru or anything I just looked for the code.
 
This should work!

System.Net.WebProxy

Dim proxyObject As New WebProxy("http://proxyserver:80/", True)
Dim req As WebRequest = WebRequest.Create("http://www.contoso.com")
req.Proxy = proxyObject

Make sure the proxy works. I spent alot of time because I did not know the proxy was bad.
 
Back
Top