简体   繁体   中英

Response in ASP.net is not showing my custom header

So I'm using ASP.NET 3.5 and learning about adding custom headers to a Request.

void Button1_Click(object sender, EventArgs e)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(my_url_here);

    webRequest.Headers.Add("Hello", "World");

    HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

    StreamReader r = new StreamReader(resp.GetResponseStream());

    Response.Write(r.ReadToEnd());

    Response.End();
}

I can use xhr.setRequestHeader through JavaScript and pick up my custom name/value in the response that way.

But, if I put a break point above after .GetResponse and inspect resp.Headers.AllKeys my custom header is not there. Why would that be?

i'm not sure I understand you'r question but it seems to me you have a mix up. if you set on client side an header you should search for it on the Request on Server side. the Response (on server) is the Response you will send back to client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM