Click here to Skip to main content
13,662,299 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 14 Apr 2002

How to create a simple proxy in Managed C++

, 20 Apr 2002
Rate this:
Please Sign up or sign in to vote.
How to create a simple proxy to parse HTTP to another server using Managed C++
<!-- Add the rest of your HTML here -->


Many people have been asking me about a how to create a proxy. Actually is very simple to do in Managed C++. This article is made very simple, you can add as much as you want at the base class we can call it CHttpProxy

I'll be using the known class  TcpClient.

<pre lang=mc++>// Header File // CHttpProxy.h __gc class CHttpProxy { public: CHttpProxy(String __gc *szHost, int port); String __gc *m_host; int m_port; unsigned char SendToServer(unsigned char Packet __gc[]) __gc[]; }; // CHttpProxy.cpp #using <mscorlib.dll> #using <SYSTEM.DLL> using namespace System; using System::Net::Sockets::TcpClient; using System::String; using System::Exception; using System::Net::Sockets::NetworkStream; #include "httpproxy.h" #include <stdio.h> CHttpProxy::CHttpProxy(String __gc *szHost, int port) { m_host = szHost; m_port = port; } unsigned char CHttpProxy::SendToServer(unsigned char Packet __gc[]) __gc[] { TcpClient * tcpclnt = new TcpClient(); unsigned char bytes __gc[]; try { tcpclnt->Connect(m_host,m_port); } catch (Exception * e ) { Console::WriteLine(e->ToString()); return NULL; } // Send it if ( tcpclnt ) { NetworkStream * networkStream = tcpclnt->GetStream(); int size = Packet->get_Length(); networkStream->Write(Packet, 0, size); bytes = new unsigned char __gc[tcpclnt->ReceiveBufferSize]; networkStream->Read(bytes, 0, (int) tcpclnt->ReceiveBufferSize); return (bytes); } return (NULL); }

Simple isn't it? This class creates a connection to the "real" IIS server.

So(...) How to use it now? Simpler than bowling water, you may remember this part of the code from my previous article:

<pre lang=mc++>TcpListener * pTcpListener; TcpListener = new TcpListener(80); TcpListener->Start(); TcpClient * pTcpClient; unsigned char sendbytes __gc[]; pTcpClient = m_TcpListener->AcceptTcpClient(); NetworkStream * networkStream = pTcpClient->GetStream(); bytes = new unsigned char __gc[tcpCl->ReceiveBufferSize]; networkStream->Read(bytes, 0, (int) tcpCl->ReceiveBufferSize); // Now we got the request Console::WriteLine(S"Packet Received"); CHttpProxy *pProxy; pProxy = new CHttpProxy(S"", 80); // Goes somewhere else! sendbytes = pProxy->SendToServer(bytes); networkStream->Write(sendbytes, 0 , sendbytes->get_Length()); networkStream->Close();

We listen on port 80 for any connection. Anything that comes in that port is re-directed to It's actually a pretty easy concept. The most interesting part comes when you get the buffer. You got the control to remove images from the HTML or change the content on the fly.

In my next article will look into those possibilities.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Albert Pascual
Web Developer
United States United States
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP


You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
Member 911174714-Jun-12 10:38
memberMember 911174714-Jun-12 10:38 
QuestionAn example ? Pin
ahmo99gmail19-Aug-08 0:53
memberahmo99gmail19-Aug-08 0:53 
QuestionDownload link Pin
Kishan Hathiwala11-Jul-08 2:36
memberKishan Hathiwala11-Jul-08 2:36 
GeneralVery good sample Pin
Member 14036344-Jun-08 8:21
memberMember 14036344-Jun-08 8:21 
QuestionSimpler than bowling water? Pin
coolman545329-Mar-07 2:35
membercoolman545329-Mar-07 2:35 
QuestionC# Example? Pin
plehxp15-Dec-05 23:35
memberplehxp15-Dec-05 23:35 
AnswerRe: C# Example? Pin
sf4all19-May-06 9:25
membersf4all19-May-06 9:25 
Try here:
BUT remember, this is not a simple proxy Roll eyes | :rolleyes:

QuestionMain Class? Pin
valente1-Nov-03 11:22
membervalente1-Nov-03 11:22 
QuestionFor VC++? Pin
Kasper B5-May-02 11:53
memberKasper B5-May-02 11:53 
AnswerRe: For VC++? Pin
Albert Pascual6-May-02 6:00
memberAlbert Pascual6-May-02 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.180810.1 | Last Updated 21 Apr 2002
Article Copyright 2002 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid