SIGN UP MEMBER LOGIN:    
ARTICLE

Web ProxyServer in C# and VB

Posted by Pramod Singh Articles | Internet & Web February 06, 2001
Web Proxy Server is HTTP proxy server written in C#.It is Multithreaded so many clients can access the web through this WebProxy Server.
Reader Level:

Introduction

Web Proxy Server is HTTP proxy server written in C#. It is Multithreaded so many clients can access the web through this WebProxy Server.

Technology Used

System.NET, System.IO ,System.Threading and C#

About the sample

This sample is a console application which acts as a multithreaded WebProxy server.If you want to use any specific port to Listen then use that as a command line parameter other wise default 8089 port is used. It is multithreade Proxy Server that means multiple client can communicate with Proxy server.  

Good luck and Enjoy C#

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
namespace WebProxy2
{
class WebProxy2
{
Socket clientSocket;
Byte[] read =
new byte[1024];
Byte [] Buffer =
null;
Encoding ASCII = Encoding.ASCII;
const string HTTP_VERSION = "HTTP/1.0";
const string CRLF = "\r\n";
Byte[] RecvBytes =
new Byte[4096];
public WebProxy2(Socket socket)
{
this.clientSocket = socket;
}
public void run()
{
String clientmessage = " ", sURL = " ";
int bytes = readmessage(read, ref clientSocket, ref clientmessage);
if(bytes == 0)
{
return;
}
int index1 = clientmessage.IndexOf(' ');
int index2 = clientmessage.IndexOf(' ', index1 + 1);
if((index1 == -1) || (index2 == -1))
{
throw new IOException();
}
Console.WriteLine("Connecting to Site: {0}", clientmessage.Substring(index1 + 1, index2 - index1));
Console.WriteLine("Connection from {0}", clientSocket.RemoteEndPoint);
string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
int index3 = part1.IndexOf('/', index1 + 8);
int index4 = part1.IndexOf(' ', index1 + 8);
int index5 = index4 - index3;
sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);
try
{
IPHostEntry IPHost = Dns.Resolve(sURL);
Console.WriteLine("Request resolved: ", IPHost.HostName);
string [] aliases = IPHost.Aliases;
IPAddress[] address = IPHost.AddressList;
Console.WriteLine(address[0]);
IPEndPoint sEndpoint =
new IPEndPoint(address[0], 80);
Socket IPsocket =
new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
IPsocket.Connect(sEndpoint);
if(IPsocket.Connected)
Console.WriteLine("Socket connect OK");
string GET = clientmessage;
Byte[] ByteGet = ASCII.GetBytes(GET);
IPsocket.Send(ByteGet, ByteGet.Length, 0);
Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
Console.WriteLine("Recieved {0}", + rBytes);
//Buffer = RecvBytes;
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes);
while (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 (Exception exc2)
{
Console.WriteLine(exc2.ToString());
}
}
private int readmessage(byte [] ByteArray, ref Socket s, ref String clientmessage)
{
int bytes = s.Receive(ByteArray, 1024, 0);
string messagefromclient = Encoding.ASCII.GetString(ByteArray);
clientmessage = (String)messagefromclient;
return bytes;
}
private void sendmessage(Socket s, string message)
{
Buffer =
new Byte[message.Length + 1];
int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}
static void Main(string[] args)
{
const int port = 8889;
TcpListener tcplistener =
new TcpListener(port);
Console.WriteLine("Listening on port {0}", + port);
tcplistener.Start();
while(true)
{
Socket socket = tcplistener.AcceptSocket();
WebProxy2 webproxy =
new WebProxy2(socket);
Thread thread =
new Thread(new ThreadStart(webproxy.run));
thread.Start();
}
}
}
}

VB.NET source code updated by Russ Green.

Login to add your contents and source code to this article
share this article :
post comment
 

Did any get this working?

Posted by Gangadhara BD Jan 05, 2011

would you please explain me how to run this code i mean i didnt know at all where to type this code in visual studio can you tell me what can i step by step to run this program cheers

Posted by Dan Jake Dec 24, 2010

Hey, I've tried wiggling this down as much as I can, but I can't get this to work. I'm guessing maybe a Windows 7 problem.


Basically the most of it works, but it comes to a problem around the sendmessage function. Is there another way we can send a message to the browser, or is it a problem with something I have setup?

Thanks

Posted by Mark Wylde Jul 10, 2010

Just wanted to thank you for some great code, helped me test a proxy issue I was having and now I am ... pretty happy lol.


Thanks again!
Matt

Posted by Matthew Rees Jun 02, 2010

thanks.

Posted by misbah alnawashi Mar 03, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor