0

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.

I figure it will be faster to get this up and running in ASP. So the question is consider a WCF function like this:

[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 

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).

Thanks!

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:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <form action="WebForm2.aspx.cs" > <asp:textbox id="Email" runat="server"/> <asp:textbox id="Password" runat="server"/> <asp:Button id="Button1" OnClick="OnButtonClick" runat="server" Text="Login"/> </form> <asp:Label ID="labelResult" runat="server" /> </asp:Content> 

Then on click in the code behind do something like this:

 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; } 

1 Answer 1

4

Basic approach to call REST service is by HttpWebRequest

// User object serialized to XML XElement data = new XElement("User", new XElement("UserName", UserName), new XElement("Password", Password) ); MemoryStream dataSream = new MemoryStream(); data.Save(dataStream); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(YourServiceUrl); request.Method = "POST"; request.ContentType = "application/xml"; // You need to know length and it has to be set before you access request stream request.ContentLength = dataStream.Length; using (Stream requestStream = request.GetRequestStream()) { dataStream.CopyTo(requestStream); requestStream.Close(); } HttpWebResponse response = request.GetResponse(); if (response.Status == HttpStatusCode.Unauthorized) { ... } else { ... } response.Close(); 
Sign up to request clarification or add additional context in comments.

2 Comments

sweet, i already have code for sending a request but will use a bit of this stuff. my question was probably not accurate then, i was just wondering if there was a "nice way" to do this or you just do this in a button click handler.
I getting this error : {"Cannot close stream until all bytes are written."} at the requestStream.close(). but adding this in using block byte[] bytes = dataSream1.ToArray(); requestStream.Write(bytes, 0, Convert.ToInt16(dataSream1.Length));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.