/ 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
Expand |
Embed | Plain Text
<%@ WebHandler Language="C#" Class="proxy" %> /* Single-file Proxy. This proxy page does not have any security checks. It is highly recommended that a user deploying this proxy page on their web server, add appropriate security checks, for example checking request path, username/password, target url, etc. */ using System; using System.Web; public class proxy : IHttpHandler { public void ProcessRequest (HttpContext context) { string uri = context.Request.QueryString[0]; req.Method = context.Request.HttpMethod; context.Request.InputStream.Read(bytes,0,(int)context.Request.InputStream.Length); req.ContentLength = bytes.Length; req.ContentType = "application/x-www-form-urlencoded"; System.IO.Stream outputStream = req.GetRequestStream(); outputStream.Write(bytes, 0, bytes.Length); outputStream.Close(); System.Net.WebResponse res = req.GetResponse(); context.Response.ContentType = res.ContentType; string strResponse = sr.ReadToEnd(); context.Response.Write(strResponse); context.Response.End(); } public bool IsReusable { get { return false; } } } /////////////////////////////////////////////////////////////////////////////// //WebFetch (class-based approach you could use standalone command-line or reuse /////////////////////////////////////////////////////////////////////////////// using System; using System.IO; using System.Net; using System.Text; /// <summary> /// Fetches a Web Page /// </summary> class WebFetch { static void Main(string[] args) { // used to build entire input // used on each read operation // prepare the web page we will be asking for HttpWebRequest request = (HttpWebRequest) if (args.Length > 0) { WebRequest.Create(args[0]); } else { WebRequest.Create("http://www.bcmoney-mobiletv.com"); } // execute the request HttpWebResponse response = (HttpWebResponse) request.GetResponse(); // we will read data via the response stream Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { // fill the buffer with data count = resStream.Read(buf, 0, buf.Length); // make sure we read some data if (count != 0) { // translate from bytes to ASCII text tempString = Encoding.ASCII.GetString(buf, 0, count); // continue building the string sb.Append(tempString); } } while (count > 0); // any more data to read? // print out page source Console.WriteLine(sb.ToString()); } }
You need to login to post a comment.
