""""Open HTTP Page With Raw Socket (C#)""""

I am new in Networking Programming.  Recently I am trying to make an application that will open an HTTP Page using Raw Socket.  I guess should prepare code for IP Header, UDP header, etc?? I have been searching the net but can't find any good example for it.  Anyone could give some suggestion? Thank you in advance.
Who is Participating?
If you mean changing the browser headers, such as User-Agent, here is an example:

    static void Main(string[] args)
      System.Net.WebClient wc;
      wc = new System.Net.WebClient();
      wc.Headers["User-Agent"] = ""; //TODO: Change User-Agent string e.g.: "Mozilla/4.0"

      System.IO.StreamReader webReader;
      webReader = new System.IO.StreamReader(wc.OpenRead("http://pgl.yoyo.org/http/browser-headers.php"));

      string webPage = webReader.ReadToEnd();

What are you trying to do, download a web page as a byte[] array, or to a string, or are you specifically trying to open and control a socket connected to port 80 of the web server?
mobanaAuthor Commented:
i am trying download a web page, but want to set my own IP Header on Packet sending.  
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

If you're new to network programming, you probably don't want to worry about IP headers.  I think only Windows XP would theoretically allow you to alter IP headers, but even then, it would be through native API calls.  As far as I know, they didn't include these capabilities as part of the .NET Framework.  If that's really what you're trying to do, you'd have to declare an external native function.

If you want to download a web page, that is another issue...  Here are two pieces of code I used to get this working in C#:

  System.Net.WebClient wc = new System.Net.WebClient();

  // This downloads the web page to a local file (in /bin/debug or /bin/release if your in VS.NET)

  // This would download that same URL to a byte array and then convert it to a UTF8 (unicode) string.  
  byte[] data = wc.DownloadData(TARGET_URL);
  string s = System.Text.Encoding.UTF8.GetString(data);

I can also help you accomplish the same thing with Sockets via the .NET framework, but it requires understanding of the HTTP protocol and won't let you tamper with IP header information.  See this page for info: http://www.faqs.org/rfcs/rfc2616.html



One other thing... HTTP is a connection-oriented protocol.  It relies on TCP/IP, not UDP.  No need to worry about UDP for this problem.  Also, have a look at the Internet Protocol.  I really doubt you want to do anything to the IP headers unless you're trying to spoof your address or something like that.  http://www.tekelec.com/ss7/protocols/ip2.asp

(sorry for the 3 signatures above)
mobanaAuthor Commented:
Thanks Sholodak.  some web browser now equipped to be able alter with User Agent, etc...things that heard taboo in early era of internet terms.  in future web browser might be equipped to be able change ip setting too,  i guess...  I am using WinXp and I heard it is possible to change IP Header of it.   it would be grateful if anyone could share some of his knowledge here...
mobanaAuthor Commented:
Thanks for the supporting post.  there is some examples about capturing raw socket, but can't one for sending raw socket.  the principle might be the same, but it was written in C language that unfamiliar for me...T.T
I tried to throw something together to try and get the WebClient effect with raw sockets.  For some reason the response body isn't showing (should be one blank line after the response headers).  I am getting 200 responses from remote servers, which tells me that the server is processing the request successfully.

This does NOT attempt to use Raw sockets, though in throwing this together, it does seem possible to do.  Whether support is available for Windows XP via .NET is still as of yet unknown to me.

// Prepare a TCP/IP Connection to www.yahoo.com port 80 (standard web server port)
// NOTE: If you want to use raw sockets, you will change System.Net.Sockets.SocketType.Stream to System.Net.Sockets.SocketType.Raw
//   this will require you to send your own valid IP header.  Search google for specifics on the IP protocol.
System.Net.Sockets.Socket s = new Socket(
System.Net.IPEndPoint iep = new System.Net.IPEndPoint(
// Prepare a byte[] containing the request
// If you're trying to emulate a particular browser, you can either get a tool to view the headers or try Etherial/EtterCap
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("GET / HTTP/1.0\r\n");
sb.Append("Connection: close\r\n");
sb.Append("Host: www.yahoo.com\r\n");
sb.Append("User-Agent: Mozilla/4.0\r\n");
sb.Append("Accept: */*\r\n");
byte[] send = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
// Send the request to the web server

// Read back the byte[] from the web server and buffer it's string equivalent into a string builder
System.Text.StringBuilder resp = new System.Text.StringBuilder();
int ct = 0;
byte[] receive = new byte[4096];
while ((ct = s.Receive(receive)) > 0) {
  receive = new byte[4096];

// Show the response in a message box.

You'll notice the WebClient solution was much more approachable, but less flexible.  I believe there is also an HttpRequest class in System.Net that you could probably use to assemble the request in a more OO-friendly way.  I think you could use that to transmit the request via sockets as I did manually above.

The second one seems like it might help, but you should probably focus on getting the fundamentals of network programming down before you try and dive into that article.  For starters, you should be aware that the HTTP is a top-level protocol.  It relies on TCP on top of IP.  If you are going to use raw sockets, I think you'll have to take care of creating both an IP header and a TCP header.  

If you are trying to spoof your IP address, you shouldn't have to change anything in the TCP headers or the HTTP headers.  If I'm not mistaken, you'll be sending out packets with another return address.  In other words, you can never expect to see a response from the HTTP server unless you use your actual IP address.  I would start off just trying to get it to work with your real IP before screwing around with it.  You should also be aware that there are some legal implications to hiding, and even trying to hide, the origin of network traffic in many states/countries.

Some additional resources:
HTTP/1.1 Request specification: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
Info on Raw Sockets in C# (IP related): http://www.c-sharpcorner.com/Network/SimpleSnifferInCSLM.asp
Another example using raw (you don't want ICMP/IGMP) Sockets: http://www.codeproject.com/csharp/trace.asp

Lots of luck

mobanaAuthor Commented:
Thousands of thanks to everyone here.  I'm still keep struggle these few days to find the answer.  Actually there is no problem when i sent a packet with ICMP protocol.  I guess the principle of sending IP Header should be the same too?  So i try this code :

Socket s= new Socket(AddressFamily.InterNetwork,
                     SocketType.Raw, ProtocolType.IP);
                           System.Net.Sockets.SocketOptionName.HeaderIncluded, 1);
s.Send(iphead_httphead, iphead_httphead.Length, 0);
int Result = s.Receive(ByteRecv, ByteRecv.Length, 0);

It display socket error that didn't get proper respon from connected host socket.  It succeed to connect with the remote host, but can't send the proper request packet?? I feel it just one step left to the right answer.  any suggestion for it?? thank you.  
First thing I've noticed is that your code doesn't mention anything about TCP.  You won't be able to successfully connect to a web server because it relies on TCP/IP.  It look as though you're just sending an IP header and an HTTP header, in which case the packet would never even make it to the application level of the web server.  

Also, make sure you are testing with your real address.  You might get an error (I believe at the TCP level) if you are sending from a spoofed address because TCP will require acknowledgements which will never arrive.  If I were you, I would set up a web server in your private network and put ettercap on it to monitor the activity.  It will let you see exactly what it is receiving and will probably indicate whether your packets are properly formed.  If your solution is working, it should show you the details of the HTTP request.  If it doesn't, your packet probably has a bad/missing header.

I think the TCP acknowledgement/sliding window problems will arise when spoofing your address unless you set up another machine to send back valid ack packets to your machine (spoofing the origin to be your actual destination).  Or I suppose you could ignore the errors on your end once your sure the packets are getting across.

I would monitor what your program is sending remotely (on a test server) & see what it reveals.  Since you are using TCP to get at a web site, and TCP creates the illusion of a connection-oriented, reliable protocol, you should expect some difficulties keeping the TCP handler on your end happy, unless of course you are just creating the TCP packets yourself and there is nothing to handle them.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.