从ASP消耗WCF Rest 4。净

时间:2010-09-07 21:59:25

标签: c# asp.net wcf wcf-rest

我是一个完整的ASP .NET新手。我使用WCF 4 Rest Starter Kit编写了一组Web服务。我在Flash应用程序中调用了所有内容,但是我想编写一个快速而脏的管理面板供我自己使用,不需要用Flash编写。

我认为在ASP中运行起来会更快。所以问题是考虑这样的WCF函数:

[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

我如何使用用户名,密码,提交框从ASP .Net页面使用它,它显示错误401等或成功(returneduser.sessionid)。

谢谢!

注意: 我知道如何在C#中通过Http调用Rest服务。这真的是一个问题,是否有一个“好方法”在ASP中应用这个或者只是做一个像这样的形式:

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

然后点击后面的代码,执行以下操作:

 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 个答案:

答案 0 :(得分:4)

调用REST服务的基本方法是通过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();