更新:2007 年 11 月
获取 Sys.Net.WebRequest 实例的 HTTP 标头。
var headers = MyWebRequest.get_headers();
返回值
一个名称/值对字典,包含随 Web 请求一起发送的 HTTP 标头。
备注
headers 属性方法返回一个可以用于设置自定义标头的字典。如果当前无任何自定义标头与 Web 请求相关联,则返回一个空字典。可以使用返回的字典设置自定义标头值。
示例
下面的示例演示如何使用 HTTP 标头。此代码摘自 WebRequest 类概述中的一个完整示例。
// This function sets an HTTP header for
// the Web request.
function WebRequestHeader()
{
// Instantiate the WebRequest object.
var wRequest = new Sys.Net.WebRequest();
// Set the request Url.
wRequest.set_url(postPage);
// Set the request verb.
wRequest.set_httpVerb("POST");
var body = "Message=Hello! Do you hear me?"
wRequest.set_body(body);
// Set the value of the HTTP header's "Content-Length".
wRequest.get_headers()["Content-Length"] = body.length;
// Set the web request completed event handler,
// for processing return data.
wRequest.add_completed(OnWebRequestCompletedHeader);
// Clear the results page element.
GetElementById("ResultsId").innerHTML = "";
// Execute the request.
wRequest.invoke();
}