Asp.net Ajax【客户端二】Sys.Net.WebRequest
先从代码开始
客户端请求页面Client.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function pageLoad() {
}
function onCompleteCallback(executor,eventArgs)
{
if(executor.get_responseAvailable())
{
var div=$get("div1");
div.innerHTML=executor.get_responseData()+"<br/><b>"
+executor.getAllResponseHeaders()+"</b>";
}
}
function call()
{
var req=new Sys.Net.WebRequest();
req.set_url("Server.aspx");
req.set_httpVerb("Post");
req.add_completed(onCompleteCallback);
var body="Name="+encodeURIComponent($get("Text1").value);
req.set_body(body);
req.get_headers()["Content-Length"]=body.length;
req.invoke();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</div>
<input id="Text1" type="text" />
<input id="Button1" type="button" value="提交" onclick="call()"/>
<div id="div1"></div>
</form>
</body>
</html>
服务器相应文件Server.aspx代码
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Write("Hello,"+Request["name"]);
}
执行结果
体会:
回调函数写法
function onCompleteCallback(executor,eventArgs)
设置POST方式请求服务器
req.set_httpVerb("Post");
将数据放到Post内部
var body="Name="+encodeURIComponent($get("Text1").value);
req.set_body(body);
Form中的get和post方法,在数据传输过程中分别对应了HTTP协议中的GET和POST方法。二者主要区别如下:
1、Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据。
2、Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接;Post是将表单中的数据放在form的数据体中,按照变量和值相对应的方式,传递到action所指向URL。
3、Get是不安全的,因为在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前。Post的所有操作对用户来说都是不可见的。
4、Get传输的数据量小,这主要是因为受URL长度限制;而Post可以传输大量的数据,所以在上传文件只能使用Post(当然还有一个原因,将在后面的提到)。
5、Get限制Form表单的数据集的值必须为ASCII字符;而Post支持整个ISO10646字符集。
6、Get是Form的默认方法。
检查XmlHttp是否返回成功
if(executor.get_responseAvailable())
以下是Sys.Net.WebRequestExecutor 类
Sys.Net.WebRequestExecutor Class Members
Name
Description
- Sys.Net.WebRequestExecutor.abort Method
Stops additional processing of the current request.
- Sys.Net.WebRequestExecutor.executeRequest Method
Executes a Web request.
- Sys.Net.WebRequestExecutor.getAllResponseHeaders Method
Gets all the response headers for the current request.
- Sys.Net.WebRequestExecutor.getResponseHeader Method
Gets the value of a specific response header based on the header's name.
- Sys.Net.WebRequestExecutor aborted Property
Gets a value indicating whether the request associated with the executor was aborted.
- Sys.Net.WebRequestExecutor responseAvailable Property
Gets a value indicating whether the request completed successfully.
- Sys.Net.WebRequestExecutor responseData Property
Gets the text representation of the response body.
- Sys.Net.WebRequestExecutor started Property
Gets a value indicating whether the executor has started processing the request.
- Sys.Net.WebRequestExecutor statusCode Property
Gets a success status code.
- Sys.Net.WebRequestExecutor statusText Property
Gets status information about a request that completed successfully.
- Sys.Net.WebRequestExecutor timedOut Property
Gets a value indicating whether the request timed out.
- Sys.Net.WebRequestExecutor xml Property
Attempts to get the response to the current request as an XMLDOM object.