在request.servervariables()中添加自定义标头

时间:2014-04-07 10:53:25

标签: c# asp.net

我必须在标头中添加HTTP_MSDN,这样每当我点击按钮时,此网址的HTTP_MSDN都应该可见。请帮我。

protected void Page_Load(object sender, EventArgs e)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
    //HttpWebRequest req = WebRequest.Create("http://wap.mobiletashan.com") as HttpWebRequest;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    request.Headers.Add("HTTP_MSDN", "0123456789");                      
}
protected void Button1_Click(object sender, EventArgs e)
{
    NameValueCollection coll;
    coll = Request.ServerVariables;
    String[] arr1 = coll.AllKeys;
    String[] arr2 = coll.GetValues(arr1[0]);
    Response.Write(Server.HtmlEncode(arr2[0]));
}

1 个答案:

答案 0 :(得分:2)

如果您需要将标头添加到请求中,请在获得响应之前添加它,如下所示:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
request.Headers.Add("HTTP_MSDN", "0123456789"); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();