求VB.Net代理示例 [英] Seeking VB.Net Proxy examples

查看:58
本文介绍了求VB.Net代理示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过 http://www.vbdotnetheaven.com/ Code / Aug2003 / 2146.asp - 但是它

不起作用。


我需要一个有效的例子....如果有人有看到一个。


Jim

解决方案

对不起我的无知,但代理服务器究竟做了什么?我不是熟悉术语代理的b $ b。感谢您提供的任何信息或链接

分享。

-

Dennis in Houston

"吉姆"写道:

我见过 http://www.vbdotnetheaven.com/Code/Aug2003/2146.asp - 但它不起作用。

我需要一个有效的例子....如果有人见过的话。

Jim





" Dennis" <德**** @ discussions.microsoft.com>在消息中写道

news:77 ********************************** @ microsof t.com ...

对不起我的无知,但代理服务器究竟做了什么?我不熟悉术语代理。感谢您分享的任何信息或链接。




代理服务器代表另一台机器(PC)发出请求。代理

服务器主要用于HTTP流量,但可用于需要中间的任何流量

。用于2台PC(例如在PC之间中继IM

对话的服务器)。


当代理用于HTTP流量时,您必须将浏览器指向

代理而不是你的有线调制解调器,路由器或直接互联网连接。

代理成为你的HTTP请求的目标。代理然后代表您将

连接到网络(或互联网),检索您请求的

页面/资源并将其传递给您。


代理可用于过滤广告,弹出窗口,病毒,成人内容,
令人反感的网站,允许匿名网上冲浪(参见 www.anonymizer.com

这样的一个例子)等等。他们也可以用来跟踪员工网站

用法(我个人反对的大哥哥)。


如需更好的解释,请参阅 http://en.wikipedia.org/wiki/Proxy_server

希望这会有所帮助。


Jim


我找到了一个VB.Net控制台代理的例子,修复了几个

错误。但是,它似乎没有通过图片并抛出错误

,上面写着现有连接被远程主机强行关闭。


目标是能够在去往浏览器的途中过滤内容。任何

帮助你可以用这个项目给予赞赏。


VB.Net 2005代码如下......

进口系统''

进口System.Net

进口System.Net.Sockets

进口System.Text

Imports System.IO

Imports System.Threading


命名空间WebProxy2


类WebProxy2

私有客户端套接字作为套接字

私有读取()作为[字节] =新字节(1024){}

私有缓冲区作为[字节]( )= Nothing

私有ASCII编码= Encoding.ASCII

私有HTTP_VERSION字符串=" HTTP / 1.0"

私有CRLF为String = ControlChars.Cr + ControlChars.Lf

Private RecvBytes(4096)As [Byte]

Public Sub New(ByVal socket as Socket)

Me.clientSocket = socket

End Sub''新


公共子运行()

Dim clientmessage As [String] =

Dim sURL As [String] ="

Dim bytes As Integer = readmessage(read,clientSocket,

clientmessage)

如果bytes = 0那么

返回

结束如果

Dim index1 As Integer = clientmessage.IndexOf(" c)

Dim index2 As Integer = clientmessage .IndexOf("" c,index1 + 1)

如果index1 = -1或index2 = -1那么

抛出新IOException()

结束如果

Console.WriteLine(连接到站点:{0},

clientmessage.Substring(index1 + 1,index2 - index1))

Console.WriteLine(从{0}连接,

clientSocket.RemoteEndPoint)


Dim part1 As String = clientmessage.Substring(index1 + 1,

index2 - index1)

Dim index3 As Integer = part1.IndexOf(" /" c,index1 + 8)

Dim index4 As Integer = part1.IndexOf("" c,index1 + 8)

Dim index5 As Integer = index4 - index3

sURL = part1.Substring(ind ex1 + 4,part1.Length - index5 - 8)


尝试

Dim IPHost As IPHostEntry = Dns.Resolve(sURL)

Console.WriteLine(" Request resolved:",IPHost.HostName)

Dim aliases As String()= IPHost.Aliases

Dim address As IPAddress() = IPHost.AddressList

Console.WriteLine(地址(0))

Dim sEndpoint为新的IPEndPoint(地址(0),80)

Dim IPsocket作为新套接字(AddressFamily.InterNetwork,

SocketType.Stream,ProtocolType.Tcp)

IPsocket.Connect(sEndpoint)

如果IPsocket .Connected然后

Console.WriteLine(Socket connect OK)

结束如果

Dim [GET] As String = clientmessage

Dim ByteGet As [Byte]()= ASCII.GetBytes([GET])

IPsocket.Send(ByteGet,ByteGet.Length,0)

Dim rBytes As Int32 = IPsocket.Receive(RecvBytes,

RecvBytes.Length,0)

Console.WriteLine(" Recieved {0}",+ rBytes)

''Buffer = RecvBytes;

Dim strRetPage As [String] = Nothing

strRetPage = strRetPage + ASCII.GetString(RecvBytes,0,

rBytes)

虽然rBytes> 0

rBytes = IPsocket.Receive(RecvBytes,RecvBytes.Length,

0)

strRetPage = strRetPage + ASCII.GetString(RecvBytes,0 ,

rBytes)

结束时

IPsocket.Shutdown(SocketShutdown.Both)

IPsocket.Close()

sendmessage(clientSocket,strRetPage)

Catch exc2 As Exception

Console.WriteLine(exc2.ToString())

结束尝试

结束子''运行


私人函数readmessage(ByVal ByteArray()As Byte,ByRef s As

Socket,ByRef clientmessage As [String])As Integer

Dim bytes As Integer = s.Receive(ByteArray,1024,0)

Dim messagefromclient As String =

Encoding.ASCII.GetString(ByteArray)

clientmessage = CType(messagefromclient,[String])

返回字节

结束函数''readmessage

Private Sub sendmessage(ByVal s As Socket,ByVal message as String)

Buffer = New [Byte](message.Lengt h + 1){}

Dim length As Integer = ASCII.GetBytes(message,0,

message.Length,Buffer,0)

s.Send(Buffer,length,0)

End Sub''sendmessage


''代表C风格主要私人功能的入口点

公共重载共享子主()

Main2(System.Environment.GetCommandLineArgs())

结束子

重载Shared Sub Main2(ByVal args()As String)

Const port As Integer = 8889

''必须为地址类型设置本地字节数组

Dim myIP(3)As Byte

myIP(0)= 127

myIP(1)= 0

myIP(2)= 0

myIP(3)= 1

Dim myLocalAddress为新的System.Net.IPAddress(myIP)


''使用字节数组打开列表器

Dim tcplistener1 As New TcpListener(myLocalAddress,port)

''TcpListener(port)

Console.WriteLine(侦听端口{0},+端口)

tcplistener1.Start()

正确

Dim socket As Socket = tcplistener1.AcceptSocket()

Dim webproxy As New WebProxy2(socket )

Dim thread As New Thread(New ThreadStart(AddressOf

webproxy.run))

thread.Start()

结束时

End Sub''Main

End Class''WebProxy2

End Namespace''WebProxy2


I have seen http://www.vbdotnetheaven.com/Code/Aug2003/2146.asp - but it
does not work.

I need a working example....if anyone has seen one.

Jim

解决方案

Sorry for my ignorance, but what does a proxy server do exactly? I am not
familiar with the term "Proxy". Thanks for any info or links that you can
share.
--
Dennis in Houston
"Jim" wrote:

I have seen http://www.vbdotnetheaven.com/Code/Aug2003/2146.asp - but it
does not work.

I need a working example....if anyone has seen one.

Jim




"Dennis" <De****@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...

Sorry for my ignorance, but what does a proxy server do exactly? I am
not
familiar with the term "Proxy". Thanks for any info or links that you can
share.



A proxy server makes requests on behalf of another machine (PC). Proxy
servers are mostly used for HTTP traffic, but can be used for any traffic
that needs a "go-between" for 2 PCs (like the servers that relay IM
conversations between PCs).

When a proxy is used for HTTP traffic, you must point your browser to the
proxy instead of to your cable modem, router or direct internet connection.
The proxy becomes the target of your HTTP requests. The proxy then connects
to the network (or internet) on your behalf, retrieves your requested
page/resource and passes it along to you.

Proxies can be used to filter out ads, pop-ups, viruses, adult content,
objectionable websites, allow anonymous web surfing (see www.anonymizer.com
for an example of this), etc.. They can also be used to track employee web
usage (a big brother kind of thing that I, personally, oppose).

For a better explanation, see http://en.wikipedia.org/wiki/Proxy_server .

Hope this helps.

Jim


I have found an example of VB.Net console proxy, and have fixed a couple of
errors. But, it does not seem to pass through pictures and throws an error
that says "An existing connection was forcibly closed by the remote host".

The goal is to be able to filter the content on the way to the browser. Any
help you could give with this project would be appreciated.

The VB.Net 2005 code is as follows......
Imports System ''
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Threading

Namespace WebProxy2

Class WebProxy2
Private clientSocket As Socket
Private read() As [Byte] = New Byte(1024) {}
Private Buffer As [Byte]() = Nothing
Private ASCII As Encoding = Encoding.ASCII
Private HTTP_VERSION As String = "HTTP/1.0"
Private CRLF As String = ControlChars.Cr + ControlChars.Lf
Private RecvBytes(4096) As [Byte]
Public Sub New(ByVal socket As Socket)
Me.clientSocket = socket
End Sub ''New

Public Sub run()
Dim clientmessage As [String] = " "
Dim sURL As [String] = " "
Dim bytes As Integer = readmessage(read, clientSocket,
clientmessage)
If bytes = 0 Then
Return
End If
Dim index1 As Integer = clientmessage.IndexOf(" "c)
Dim index2 As Integer = clientmessage.IndexOf(" "c, index1 + 1)
If index1 = -1 Or index2 = -1 Then
Throw New IOException()
End If
Console.WriteLine("Connecting to Site: {0}",
clientmessage.Substring(index1 + 1, index2 - index1))
Console.WriteLine("Connection from {0}",
clientSocket.RemoteEndPoint)

Dim part1 As String = clientmessage.Substring(index1 + 1,
index2 - index1)
Dim index3 As Integer = part1.IndexOf("/"c, index1 + 8)
Dim index4 As Integer = part1.IndexOf(" "c, index1 + 8)
Dim index5 As Integer = index4 - index3
sURL = part1.Substring(index1 + 4, part1.Length - index5 - 8)

Try
Dim IPHost As IPHostEntry = Dns.Resolve(sURL)
Console.WriteLine("Request resolved: ", IPHost.HostName)
Dim aliases As String() = IPHost.Aliases
Dim address As IPAddress() = IPHost.AddressList
Console.WriteLine(address(0))
Dim sEndpoint As New IPEndPoint(address(0), 80)
Dim IPsocket As New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
IPsocket.Connect(sEndpoint)
If IPsocket.Connected Then
Console.WriteLine("Socket connect OK")
End If
Dim [GET] As String = clientmessage
Dim ByteGet As [Byte]() = ASCII.GetBytes([GET])
IPsocket.Send(ByteGet, ByteGet.Length, 0)
Dim rBytes As Int32 = IPsocket.Receive(RecvBytes,
RecvBytes.Length, 0)
Console.WriteLine("Recieved {0}", +rBytes)
''Buffer = RecvBytes;
Dim strRetPage As [String] = Nothing
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0,
rBytes)
While rBytes > 0
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length,
0)
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0,
rBytes)
End While
IPsocket.Shutdown(SocketShutdown.Both)
IPsocket.Close()
sendmessage(clientSocket, strRetPage)
Catch exc2 As Exception
Console.WriteLine(exc2.ToString())
End Try
End Sub ''run

Private Function readmessage(ByVal ByteArray() As Byte, ByRef s As
Socket, ByRef clientmessage As [String]) As Integer
Dim bytes As Integer = s.Receive(ByteArray, 1024, 0)
Dim messagefromclient As String =
Encoding.ASCII.GetString(ByteArray)
clientmessage = CType(messagefromclient, [String])
Return bytes
End Function ''readmessage
Private Sub sendmessage(ByVal s As Socket, ByVal message As String)
Buffer = New [Byte](message.Length + 1) {}
Dim length As Integer = ASCII.GetBytes(message, 0,
message.Length, Buffer, 0)
s.Send(Buffer, length, 0)
End Sub ''sendmessage

''Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main2(System.Environment.GetCommandLineArgs())
End Sub
Overloads Shared Sub Main2(ByVal args() As String)
Const port As Integer = 8889

''must set up local byte array for address type
Dim myIP(3) As Byte
myIP(0) = 127
myIP(1) = 0
myIP(2) = 0
myIP(3) = 1
Dim myLocalAddress As New System.Net.IPAddress(myIP)

''use byte array to open a listner
Dim tcplistener1 As New TcpListener(myLocalAddress, port)
''TcpListener(port)

Console.WriteLine("Listening on port {0}", +port)
tcplistener1.Start()
While True
Dim socket As Socket = tcplistener1.AcceptSocket()
Dim webproxy As New WebProxy2(socket)
Dim thread As New Thread(New ThreadStart(AddressOf
webproxy.run))
thread.Start()
End While
End Sub ''Main
End Class ''WebProxy2
End Namespace ''WebProxy2


这篇关于求VB.Net代理示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆