Posted By

bcmoney on 12/10/10


Tagged

http proxy c#


Versions (?)

proxy.cs


 / Published in: C#
 

URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/

Fetching Web Pages with HTTP: http://www.csharp-station.com/HowTo/HttpWebFetch.aspx

Command-line argument processing in C#: http://en.csharp-online.net/CSharp_FAQ%3A_How_process_command_line_arguments

Fast, Scalable, Streaming AJAX Proxy - continuously deliver data from across domains: http://www.codeproject.com/KB/ajax/ajaxproxy.aspx

Since I still suck at C#, the standalone version was partially lifted (shamelessly) from Snipplr itself... its one of the best examples I could find: http://snipplr.com/view.php?codeview&id=17986

  1. <%@ WebHandler Language="C#" Class="proxy" %>
  2. /*
  3.   Single-file Proxy.
  4.   This proxy page does not have any security checks. It is highly recommended
  5.   that a user deploying this proxy page on their web server, add appropriate
  6.   security checks, for example checking request path, username/password, target
  7.   url, etc.
  8. */
  9. using System;
  10. using System.Web;
  11.  
  12. public class proxy : IHttpHandler {
  13.  
  14. public void ProcessRequest (HttpContext context) {
  15.  
  16. string uri = context.Request.QueryString[0];
  17. System.Net.WebRequest req = System.Net.WebRequest.Create(new Uri(uri));
  18. req.Method = context.Request.HttpMethod;
  19.  
  20. byte[] bytes = new byte[context.Request.InputStream.Length];
  21. context.Request.InputStream.Read(bytes,0,(int)context.Request.InputStream.Length);
  22. req.ContentLength = bytes.Length;
  23. req.ContentType = "application/x-www-form-urlencoded";
  24. System.IO.Stream outputStream = req.GetRequestStream();
  25. outputStream.Write(bytes, 0, bytes.Length);
  26. outputStream.Close();
  27.  
  28. System.Net.WebResponse res = req.GetResponse();
  29. context.Response.ContentType = res.ContentType;
  30.  
  31. System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream());
  32. string strResponse = sr.ReadToEnd();
  33. context.Response.Write(strResponse);
  34. context.Response.End();
  35. }
  36.  
  37. public bool IsReusable {
  38. get {
  39. return false;
  40. }
  41. }
  42. }
  43.  
  44. ///////////////////////////////////////////////////////////////////////////////
  45. //WebFetch (class-based approach you could use standalone command-line or reuse
  46. ///////////////////////////////////////////////////////////////////////////////
  47. using System;
  48. using System.IO;
  49. using System.Net;
  50. using System.Text;
  51.  
  52.  
  53. /// <summary>
  54. /// Fetches a Web Page
  55. /// </summary>
  56. class WebFetch {
  57. static void Main(string[] args) {
  58. // used to build entire input
  59. StringBuilder sb = new StringBuilder();
  60.  
  61. // used on each read operation
  62. byte[] buf = new byte[8192];
  63.  
  64. // prepare the web page we will be asking for
  65. HttpWebRequest request = (HttpWebRequest)
  66. if (args.Length > 0) {
  67. WebRequest.Create(args[0]);
  68. }
  69. else {
  70. WebRequest.Create("http://www.bcmoney-mobiletv.com");
  71. }
  72. // execute the request
  73. HttpWebResponse response = (HttpWebResponse)
  74. request.GetResponse();
  75.  
  76. // we will read data via the response stream
  77. Stream resStream = response.GetResponseStream();
  78.  
  79. string tempString = null;
  80. int count = 0;
  81.  
  82. do {
  83. // fill the buffer with data
  84. count = resStream.Read(buf, 0, buf.Length);
  85.  
  86. // make sure we read some data
  87. if (count != 0) {
  88. // translate from bytes to ASCII text
  89. tempString = Encoding.ASCII.GetString(buf, 0, count);
  90.  
  91. // continue building the string
  92. sb.Append(tempString);
  93. }
  94. }
  95. while (count > 0); // any more data to read?
  96.  
  97. // print out page source
  98. Console.WriteLine(sb.ToString());
  99. }
  100. }

Report this snippet  

You need to login to post a comment.