Skip to content

Instantly share code, notes, and snippets.

@dangerouse
Created January 16, 2012 22:15
Show Gist options
  • Save dangerouse/1623331 to your computer and use it in GitHub Desktop.
Save dangerouse/1623331 to your computer and use it in GitHub Desktop.
Cloudsponge Proxy Reference - .Net
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CloudSpongeProxy._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<asp:Label ID="lblContent" runat="server"></asp:Label>
</body>
</html>
using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
namespace CloudSpongeProxy
{
public partial class _Default : System.Web.UI.Page
{
string targetUrl = "https://api.cloudsponge.com/auth";
protected void Page_Load(object sender, EventArgs e)
{
lblContent.Text = HttpProxy(targetUrl, Request);
}
/// <summary>
/// Proxies a web request to the given URI without following redirection.
/// If the URI returns a redirect response, the redirect is returned.
/// </summary>
/// <param name="endpoint">The URI to proxy a call to</param>
/// <param name="origRequest">The request to be proxied</param>
/// <returns>The content from the endpoint</returns>
private string HttpProxy(string endpoint, HttpRequest origRequest)
{
// append the incoming querystring to the CloudSponge auth endpoint
string uri = endpoint + "?" + origRequest.QueryString;
// format the form data as: name1=value1&name2=value2&..
string bodyParameters = origRequest.Form.ToString();
string method = origRequest.HttpMethod;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
// Don't follow redirects, api.cloudsponge.com should return a redirect that should
// be passed to the client UA
webRequest.AllowAutoRedirect = false;
webRequest.Method = method;
if (method == "POST")
{
// WindowsLive POSTs the token, so this page should POST to api.cloudsponge.com
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes(bodyParameters);
Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; // Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); // Send it
}
catch (WebException ex)
{
return "An error occurred while importing contacts: " + ex.Message;
}
finally
{
if (os != null)
{
os.Close();
}
}
}
try
{
// get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{
return "An error occurred while importing contacts.";
}
// typically, this should redirect
if (webResponse.Headers["Location"] != null)
{
Response.Redirect(webResponse.Headers["Location"], true); // redirect and end processing now, nothing after this line will be executed.
}
// If we got here, the response didn't redirect so read the response and render it in the page.
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
return "An error occurred while importing contacts: " + ex.Message;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment