using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Text;
public partial class Process : System.Web.UI.Page
{
string domainAuthToken;
string ownerHandle;
string responseCode;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
domainAuthToken = null;
ownerHandle = null;
responseCode = null;
//Go through the posted form and extract the values we care about
System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
String nextKey = postedValues.AllKeys[i];
switch (nextKey)
{
case "ResponseCode":
responseCode = postedValues[i];
break;
case "OwnerHandle":
ownerHandle = postedValues[i];
break;
case "DomainAuthenticationToken":
domainAuthToken = postedValues[i];
break;
default:
break;
}
}
//Display the values retrieved
//Note that form control state is preserved across postbacks, so we can use these later
Label1.Text = responseCode;
Label2.Text = ownerHandle;
Label3.Text = domainAuthToken;
//Enable the next step if the request was approved
if ("RequestApproved" == responseCode)
{
//OK, can get information with ownerHandle and domainAuthToken
Button1.Enabled = true;
}
else
{
//error
Button1.Enabled = false;
}
}//!IsPostBack
}//Page_Load
protected void Button1_Click(object sender, EventArgs e)
{
//Get the address book. In a real site, this might be done as part of the Page Load handler.
//refresh variables from the form controls because there has been a postback from the button click
ownerHandle = Label2.Text;
domainAuthToken = Label3.Text;
//Use the owner handle as part of the request URI
string uri = "https://cumulus.services.live.com/" + ownerHandle + "/LiveContacts";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent = "Microsoft Windows Live data demo"; //customize this for your own application
request.Method = "GET";
//Put the domain token into a request header.
//This is one of three posssible authentication options.
request.Headers.Add("Authorization", "DomainAuthToken at=\"" + domainAuthToken + "\"");
//Issue the HTTP GET request to Windows Live Contacts.
try
{
//Make a synchronous request to Windows Live Contacts for demonstration purposes.
//On a real site, it might be better to make an asynchronous request,
// using BeginGetResponse. That requires an AsyncCallback delegate.
//You might be tempted to try this on the client in JavaScript using XMLHttpRequest.
// Don't. It doesn't currently work, since the client doesn't have the correct domain.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//The request succeeded if we got this far, so process the response.
Label4.Text = "Request succeeded";
//The response body is XML: read the stream into an XML Document.
XmlDocument contacts = new XmlDocument();
contacts.LoadXml(new StreamReader(response.GetResponseStream()).ReadToEnd());
//Show the contacts in an HTML list for demonstration purposes.
//On a real site, it might be preferable to bind the returned XML into a control for
// navigation and manipulation by the user, for instance using a TreeView with an XMLDataSource.
StringBuilder sb = new StringBuilder();
FillTree(contacts.ChildNodes, sb);
Label5.Text = sb.ToString();
response.Close();
}//try
catch (WebException ex)
{ //The HTTP request failed, so handle the error, or at least display what happened.
Label4.Text = "Request failed - " + Environment.NewLine + ex.Message;
}//catch WebException
catch (Exception ex)
{ //The parsing failed, so handle the error, or at least display what happened.
Label5.Text = "Parsing failed - " + Environment.NewLine + ex.Message;
}//catch Exception
}//Button1_Click
//recursive method to walk an XML node list and generate an HTML list
private void FillTree(XmlNodeList nodeList, StringBuilder sb)
{
sb.Append("<ul>");
foreach (XmlNode node in nodeList)
{
switch (node.NodeType)
{
case XmlNodeType.Element:
sb.Append("<li>" + node.Name + "</li>");
if (node.HasChildNodes)
{
FillTree(node.ChildNodes, sb);
}
break;
case XmlNodeType.Text:
sb.Append("<li>" + node.Value + "</li>");
break;
default:
break;
}
}
sb.Append("</ul>");
}//FillTree
}//Process (partial) class