How can I make a VERY simple web proxy using ASP.NET?

marclar picture marclar · Aug 15, 2009 · Viewed 22.3k times · Source

I'm about to launch a site that was working well until I found the following hiccup:

I can't request a Yahoo! Pipe over SSL.

So the pages that require SSL are now missing a piece of their functionality unless I figure out a way around this; obviously, this could be done if I use an SSL-hosted page on my app to request the Yahoo! pipe for me.

I've seen solutions like http://www.iisproxy.net/license.html, but it seems to be a bit heavy for what I'm trying to do.

Can't I do this with a simple ASHX handler? Or is it more complex than that?

Thank you,

Michael

Answer

marclar picture marclar · Sep 12, 2009

Thank you, John -- in case it's helpful to anyone else, here's the code I'm using in my ASHX file:

   public override void ProcessRequest(HttpContext context)
    {
        var strURL = context.Server.UrlDecode(context.Request["url"]);

        WebResponse objResponse = default(WebResponse);
        WebRequest objRequest = default(WebRequest);
        string result = null;
        objRequest = HttpWebRequest.Create(strURL);
        objResponse = objRequest.GetResponse();
        StreamReader sr = new StreamReader(objResponse.GetResponseStream());
        result = sr.ReadToEnd();
        //clean up StreamReader 
        sr.Close();

        //WRITE OUTPUT
        context.Response.ContentType = "application/json";
        context.Response.Write(result);
        context.Response.Flush();

    }

However, I was getting a couple extra characters (as opposed to the version that came direct from Yahoo! Pipes), so I had to remove those before parsing the JSON.