rtest.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Usage:
  3. // rtest host port url
  4. //
  5. // This program dumps the results of the HTTP request without any HTTP
  6. // headers
  7. //
  8. using System;
  9. using System.Net;
  10. using System.IO;
  11. using System.Net.Sockets;
  12. class X {
  13. static NetworkStream ns;
  14. static StreamWriter sw;
  15. static StreamReader sr;
  16. static TcpClient c;
  17. static bool debug;
  18. static bool headers;
  19. static string header;
  20. static void send (string s)
  21. {
  22. if (debug)
  23. Console.WriteLine (s);
  24. sw.Write (s);
  25. sw.Flush ();
  26. }
  27. static void Main (string [] args)
  28. {
  29. int i = 0;
  30. while (args [i].StartsWith ("-")){
  31. if (args [i] == "-debug")
  32. debug = true;
  33. if (args [i] == "-headers")
  34. headers = true;
  35. if (args [i] == "-header")
  36. header = args [++i];
  37. i++;
  38. }
  39. c = new TcpClient (args [i], Int32.Parse (args [i+1]));
  40. c.ReceiveTimeout = 1000;
  41. ns = c.GetStream ();
  42. sw = new StreamWriter (ns);
  43. sr = new StreamReader (ns);
  44. string host = args [i];
  45. if (args [i+1] != "80")
  46. host += ":" + args [i+1];
  47. send (String.Format ("GET {0} HTTP/1.1\r\nHost: {1}\r\n\r\n", args [i+2], host));
  48. MemoryStream ms = new MemoryStream ();
  49. try {
  50. byte [] buf = new byte [1024];
  51. int n;
  52. while ((n = ns.Read (buf, 0, 1024)) != 0){
  53. ms.Write (buf, 0, n);
  54. }
  55. } catch {}
  56. ms.Position = 0;
  57. sr = new StreamReader (ms);
  58. string s;
  59. while ((s = sr.ReadLine ()) != null){
  60. if (s == ""){
  61. if (headers)
  62. return;
  63. string x = sr.ReadToEnd ();
  64. Console.Write (x);
  65. break;
  66. } else {
  67. if (debug || headers)
  68. Console.WriteLine (s);
  69. if (header != null && s.StartsWith (header)){
  70. Console.WriteLine (s);
  71. return;
  72. }
  73. }
  74. }
  75. }
  76. }