Hello all!
I am trying to develop some kind of proxy which filters the data the
browser
sents to a webserver (www.google.de, www.gmx.net, ...). For this
reason I set a Proxy Connection in my IE 6.0 Settings at port 8000
(e.g) and the server to localhost (the proxy that runs local). Now I
use the TCPListener which listens and recieves data after I typed in
and pressed the send button from the browser. After this I take the
data and send it via Socket Connection to the url which was listed in
the data from the browser. Then I receieve the response content from
the webserver and want to send it back to the browser, but something
in my sourcecode isnt working right. I sitting here for days with no
idea.
Here is the code so far:
private string ConnectToURL(st ring server){
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 80;
string content="";
IPHostEntry hostInfo = Dns.Resolve(ser ver);
IPAddress[] IPaddresses = hostInfo.Addres sList;
for (int i=0; i<IPaddresses.L ength; i++){
hostAddress = IPaddresses[i];
hostEndPoint = new IPEndPoint(host Address, conPort);
s = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
s.Connect(hostE ndPoint);
if(s.Connected) {
byte[] bytes = Encoding.ASCII. GetBytes(this.t extBox1.Text);
s.Send(bytes);
while(s.Receive (bytes)>0) {
content += Encoding.ASCII. GetString(bytes );
}
s.Shutdown(Sock etShutdown.Both );
s.Close();
}
}
return content;
}
while(true){
try{
/*************** ************/
/* get header from browser */
/*************** ************/
this.tcpListene r.Start();
this.socketToCl ient = this.tcpListene r.AcceptSocket( );
byte[] bytes = new byte[1024];
this.socketToCl ient.Receive(by tes, 0,
this.socketToCl ient.Available, SocketFlags.Non e);
this.tcpListene r.Stop();
string header, host;
header = Encoding.ASCII. GetString(bytes );
host = header.Substrin g(header.IndexO f("Host: ")+6);
host = host.Substring( 0, host.IndexOf("\ n"));
this.textBox1.T ext = header.ToString ().Trim();
/*************** **************/
/* Request data from browser */
/*************** **************/
string content =ConnectToURL(h ost.Trim());
/*************** **************/
/* Send data back to browser */
/*************** **************/
byte[] bytes2 = ec.GetBytes(con tent);
this.socketToCl ient.Send(bytes 2);
if(this.socketT oClient.Connect ed){
this.socketToCl ient.Shutdown(S ocketShutdown.B oth);
this.socketToCl ient.Close();
}
}
catch (Exception e){
MessageBox.Show (e.ToString());
}
}
thank you in advance