【问题标题】:Where does Request and Response in ASP.NET come from?ASP.NET 中的请求和响应从何而来?
【发布时间】:2011-09-30 14:20:37
【问题描述】:

我觉得这是一个相当简单的问题,但我似乎无法弄清楚。我了解如何使用 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

Request 和 Response 已经被声明并且只是存在。

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

我不明白为什么我会遇到这么多麻烦。我在这里问了一个类似的问题:How can I use ASP.NET to check if cookies are enabled without having a web page? 所以要么我遗漏了一些非常明显的东西,要么我试图解决的问题非常不标准。

感谢您的帮助。

编辑:

我正在尝试做这样的事情:

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

但是 Cookies.Add 不会接受 Httpcookie,当我使用普通 cookie 时它不会被添加。

【问题讨论】:

    标签: c# asp.net cookies httpwebrequest


    【解决方案1】:

    请记住,在 ASP.Net 中,Page_Load() 方法(以及网页上的任何其他方法)是类的成员,并且该类继承自 another class。该基类的properties list 包括RequestResponse

    至于问题的后半部分,请查找 Context 变量。它已经以类似于网页中请求和响应的方式为您的 Web 服务定义,并且它将使您能够访问这些属性,包括请求中的任何 cookie。

    【讨论】:

    • 所以因为我没有使用事件驱动的方法,所以我不继承响应或请求?
    • @Nathan:不,这是因为您不在派生自 Page 的类中。我认为 Joel 提到 Page_Load 时混淆了这个问题。忘记这一点,只记得你的网页继承自 System.Web.UI.Page
    • 我不会使用网页。我只是在写一个网络服务。有了这个,我需要遵循一个特殊的协议来发出我的网络请求,并且这样的行为与他们对页面的行为相同吗?
    • @Joel - 这正是我想要的。谢谢。
    【解决方案2】:

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

    标准 Web 服务将继承自 System.Web.Services.WebService。这没有在其上声明的请求和响应。但是,它确实有一个“上下文”属性,它是一个 HTTPContext 对象,它定义了响应和请求的属性。

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

    【讨论】:

    • 这就是我所需要的。也谢谢你。
    • 没有问题。当我发布我的帖子时,我实际上并没有注意到乔尔已经更新了他的答案,提到了一些关于服务的内容,但很高兴你还是很欣赏它。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多