using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.Script.Serialization; // needed for JSON serializers
using
System.IO; // needed for Stream and Stream Reader
using
System.Net; // needed for the Web Request
using
Core2WebAPI; // needed for the Team class
namespace WebAPIClient
{
public partial class WebAPISessionExample_Results : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
// Create an
HTTP Web Request and get the HTTP Web Response from the server.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://cis-iis2.temple.edu/users/pascucci/CIS3342/CoreWebAPI/api/Vote/GetVotes/");
// Get the
current session's cookie container and restore it.
if (Session["WebRequestCookies"] != null)
{
request.CookieContainer = (CookieContainer)Session["WebRequestCookies"];
}
else
{
// Create
the cookie container to store cookies for this session.
request.CookieContainer = new CookieContainer();
Session["WebRequestCookies"] =
request.CookieContainer;
}
WebResponse response = request.GetResponse();
// Read the
data from the Web Response, which requires working with streams.
Stream theDataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(theDataStream);
String data = reader.ReadToEnd();
reader.Close();
response.Close();
// Deserialize
a JSON string into an integer.
JavaScriptSerializer js = new JavaScriptSerializer();
int count = js.Deserialize<int>(data);
lblDisplay.Text = "The Web API recorded " + count
+ " votes for your session.";
}
}
}