在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
在服务器端判断request来自Ajax请求(异步)还是传统请求(同步):
两种请求在请求的Header不同,Ajax 异步请求比传统的同步请求多了一个头参数
1、传统同步请求参数
accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-charset gb2312,utf-8;q=0.7,*;q=0.7
accept-encoding gzip,deflate
accept-language zh-cn,zh;q=0.5
cache-control max-age=0
connection keep-alive
cookie JSESSIONID=1A3BED3F593EA9747C9FDA16D309AF6B
host 192.168.101.72:8080
keep-alive 300
referer http://192.168.101.72:8080/htfsweb/spring_security_login
user-agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)
2、Ajax 异步请求方式
accept */*
accept-language zh-cn
referer http://192.168.101.72:8080/scm/?token=3b194bff23bf6acecea5661ac8e14c51
x-requested-with XMLHttpRequest
content-type application/x-www-form-urlencoded,text/javascript
accept-encoding gzip, deflate
user-agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; QQDownload 598; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.1)
host 192.168.101.72:8080
content-length 233
connection Keep-Alive
cache-control no-cache
cookie CSS=undefined; JSESSIONID=1B9AC25036290F7FB6823CCE1A24E541
可以看到 Ajax 请求多了个 x-requested-with ,可以利用它,request.getHeader("x-requested-with"); 为 null,则为传统同步请求,为 XMLHttpRequest,则为 Ajax 异步请求。
request.getHeader("x-requested-with");
为 null,则为传统同步请求;
为 XMLHttpRequest,则为 Ajax 异步请求。
http://wenwen.soso.com/z/q215517096.htm
http://www.360doc.com/content/10/0711/23/19147_38380845.shtml
private string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy(ProxyString, true); req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST"; byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length; System.IO.Stream os = req.GetRequestStream ();
os.Write (bytes, 0, bytes.Length);
os.Close (); System.Net.WebResponse resp = req.GetResponse();
if (resp== null) return null; System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
这个类型的,也就是异步发送的怎么模拟啊.这个页头怎么添加啊,不添加返回服务器错误.
bool isAjaxRequest = (sheade != null && sheader == "XMLHttpRequest") ? true : false;
哥哥,我是winform模拟Http协议发送啊,是要添加头不是获取头啊,是不是异步无法模拟发送啊?
网站留言,是直接这样加吗Headers.Add("X-Requested-With", "XMLHttpRequest");
还是返回错误啊,可能cookie有判断吧.
在页面已加载后从服务器请求数据
在页面已加载后从服务器接收数据
在后台向服务器发送数据所有现代的浏览器都支持 XMLHttpRequest 对象。
http://www.w3school.com.cn/xml/xml_http.asp
ProtocolVersion = new Version("1.0");