请求和响应在ASP中的位置.NET的来源

本文关键字:位置 NET 求和 响应 ASP 请求 | 更新日期: 2023-09-27 18:01:33

我觉得这是一个相当简单的问题,但是我似乎弄不明白。我了解如何使用HttpWebRequest创建一个webRequest,将其发送到服务器,并处理响应。

在微软的ASP。. NET示例,如:

protected void Page_Load(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    // Get cookie from the current request.
    HttpCookie cookie = Request.Cookies.Get("DateCookieExample");
    // Check if cookie exists in the current request.
    if (cookie == null)
    {
        sb.Append("Cookie was not received from the client. ");
        sb.Append("Creating cookie to add to the response. <br/>");
        // Create cookie.
        cookie = new HttpCookie("DateCookieExample");
        // Set value of cookie to current date time.
        cookie.Value = DateTime.Now.ToString();
        // Set cookie to expire in 10 minutes.
        cookie.Expires = DateTime.Now.AddMinutes(10d);
        // Insert the cookie in the current HttpResponse.
        Response.Cookies.Add(cookie);
    }
    else
    {
        sb.Append("Cookie retrieved from client. <br/>");
        sb.Append("Cookie Name: " + cookie.Name + "<br/>");
        sb.Append("Cookie Value: " + cookie.Value + "<br/>");
        sb.Append("Cookie Expiration Date: " + 
            cookie.Expires.ToString() + "<br/>");
    }
    Label1.Text = sb.ToString();
}

(http://msdn.microsoft.com/en-us/library/system.web.httpcookie.aspx)

请求和响应已经被声明并且只是存在。

我正在开发一个web服务而不是一个完整的网站。这就是为什么我没有看到已经定义的请求和响应吗?

我不明白为什么我在这方面有这么多麻烦。我在这里问了一个类似的问题:我如何使用ASP ?. NET检查cookie是否启用而没有网页?因此,要么我遗漏了一些非常明显的东西,要么我试图解决的问题非常不标准。

谢谢你的帮助。

编辑:

我想做这样的事情:

    [WebMethod]
    public bool CookiesEnabledOnClient()
    {
        bool retVal = true;
        var request = (HttpWebRequest)WebRequest.Create("http://www.dealerbuilt.com");
        request.Method = "Head";
        var response = (HttpWebResponse)request.GetResponse();
        HttpCookie Httpcookie = new HttpCookie("CookieAccess", "true");
        response.Cookies.Add(Httpcookie);      
        //If statement checking if cookie exists.
        return retVal;
    }

但饼干。Add将不接受Httpcookie,当我使用正常的cookie时,它不会被添加。

请求和响应在ASP中的位置.NET的来源

记住,在ASP中。Page_Load()方法(以及网页上的任何其他方法)是类的成员,该类继承自另一个类。该基类的属性列表包括Request和Response。

对于你的问题的后一部分,寻找Context变量。它已经为您的web服务以类似于web页面中的请求和响应的方式定义,并且它将允许您访问这些属性,包括请求中的任何cookie。

你的问题是,你的代码是在一个类继承从System.Web.UI.Page。在这个基类中有Request和Response对象,因此它们在派生类中可用。

标准的web服务将继承System.Web.Services.WebService。它没有声明请求和响应。然而,它确实有一个"Context"属性,它是一个HTTPContext对象,它定义了响应和请求的属性。

我不确定与标准网页相比,服务中的这些对象可能有什么不同,但我猜核心内容是相同的。我不知道为什么他们不在WebService类本身定义它们…