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

Posted on 2004-04-20
Medium Priority
Last Modified: 2007-12-19
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.
Question by:mobana
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
LVL 10

Expert Comment

ID: 10874854
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?

Author Comment

ID: 10874892
i am trying download a web page, but want to set my own IP Header on Packet sending.  

Expert Comment

ID: 10875540
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



What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 10875558
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)

Author Comment

ID: 10875656
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...
LVL 23

Expert Comment

ID: 10876053
LVL 10

Accepted Solution

eternal_21 earned 462 total points
ID: 10876054
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();


Author Comment

ID: 10876134
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

Expert Comment

ID: 10883372
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

LVL 23

Assisted Solution

rama_krishna580 earned 459 total points
ID: 10885810

Author Comment

ID: 10898172
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.  

Assisted Solution

sholodak earned 459 total points
ID: 10902322
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.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question