title: Consuming WCF Rest 4 From ASP . NET
body: <p>I am a complete ASP .NET newbie. I've written a set of web services using the WCF 4 Rest Starter Kit. I call everything from within a Flash application but I want to write a quick and dirty admin panel for myself to use which has no need to be written in Flash.</p>
  
  <p>I figure it will be faster to get this up and running in ASP. So the question is consider a WCF function like this:</p>
  
  <pre><code>[WebInvoke(UriTemplate = "/Login/", Method = "POST")]
          public User Login(User user)
          {
               // Code here
               // Either throw a WebFaultException or return the logged in user with a session id
  </code></pre>
  
  <p>How would I consume this from an ASP .Net page with a username, password, submit box and it either displays errors 401's etc or success (returneduser.sessionid).</p>
  
  <p>Thanks!</p>
  
  <p>Note: 
  I am aware of how to call a Rest service over Http in C#. It's really a question of is there a "nice way" to due this in ASP or is it just make a form like:</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;form action="WebForm2.aspx.cs" &gt;
      &lt;asp:textbox id="Email" runat="server"/&gt;
      &lt;asp:textbox id="Password" runat="server"/&gt;
      &lt;asp:Button id="Button1" OnClick="OnButtonClick" runat="server" Text="Login"/&gt;
    &lt;/form&gt;
    &lt;asp:Label ID="labelResult" runat="server" /&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Then on click in the code behind do something like this:</p>
  
  <pre><code> protected  void OnButtonClick(object sender, EventArgs e)
          {
              HttpWebRequest req = WebRequest.Create("http://localhost:35810/Users/Login/") as HttpWebRequest;
  
              String userString = UsefulStuff.Serialization.SerializationUtil.
                  SerializeDataContractToString(typeof(User), new User() { Email =  new Email(textboxUsername.text),
                                                                           Password = new Password(textboxPassword.text) });
  
              String strResponse = GetHttpPostResponse(req, userString);
  
              User recievedUser = UsefulStuff.Serialization.SerializationUtil.DeserializeDataContractString(
                  typeof(User), strResponse) as User;
  
              labelResult.Text = recievedUser.SessionId;
          }
  
  
          public static String GetHttpPostResponse(HttpWebRequest httpWebRequest, String serializedPayload)
          {
              httpWebRequest.Method = "POST";
              httpWebRequest.ContentType = "text/xml";
              httpWebRequest.ContentLength = serializedPayload.Length;
  
              StreamWriter streamOut = new StreamWriter(httpWebRequest.GetRequestStream(), Encoding.ASCII);
              streamOut.Write(serializedPayload);
              streamOut.Close();
  
              StreamReader streamIn = new StreamReader(httpWebRequest.GetResponse().GetResponseStream());
  
              string strResponse = streamIn.ReadToEnd();
              streamIn.Close();
  
              return strResponse;
          }
  </code></pre>