在服务器端判断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 异步请求。

解决方案 »

  1.   

    x-requested-with  XMLHttpRequest是Ajax 异步请求方式使用
    request.getHeader("x-requested-with");
     为 null,则为传统同步请求;
    为 XMLHttpRequest,则为 Ajax 异步请求。
      

  2.   

    jsp中的对象
    http://wenwen.soso.com/z/q215517096.htm
      

  3.   

    那你参考这里吧
    http://www.360doc.com/content/10/0711/23/19147_38380845.shtml
      

  4.   


    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();
    }
      

  5.   

    这个知道 啊,就是不知道x-requested-with: XMLHttpRequest
    这个类型的,也就是异步发送的怎么模拟啊.这个页头怎么添加啊,不添加返回服务器错误.
      

  6.   

    判断是否为ajaxRequest 在http请求头中查找是否包含X-Requested-With,并且值为XMLHttpRequeststring sheader = Request.header["X-Requested-With"];
    bool isAjaxRequest = (sheade != null && sheader == "XMLHttpRequest") ? true : false;
      

  7.   


    哥哥,我是winform模拟Http协议发送啊,是要添加头不是获取头啊,是不是异步无法模拟发送啊?
      

  8.   

    你想干嘛?实现模拟POST发送请求登录网站 ???
      

  9.   


    网站留言,是直接这样加吗Headers.Add("X-Requested-With", "XMLHttpRequest");
    还是返回错误啊,可能cookie有判断吧.
      

  10.   

    什么是 XMLHttpRequest 对象?XMLHttpRequest 对象用于在后台与服务器交换数据。XMLHttpRequest 对象是开发者的梦想,因为您能够:    在不重新加载页面的情况下更新网页
        在页面已加载后从服务器请求数据
        在页面已加载后从服务器接收数据
        在后台向服务器发送数据所有现代的浏览器都支持 XMLHttpRequest 对象。
    http://www.w3school.com.cn/xml/xml_http.asp
      

  11.   

    搞定了,加个这样的设置就可以了
    ProtocolVersion = new Version("1.0");
      

  12.   

    如果发送的是ajax同步请求呢(即async:false,)那么,X-Requested-With有值吗
      

  13.   

    System.Web.WebPages namespace里的WebPageRenderingBase类有个属性,叫做IsAjax.就是用来判断是否存在这个头的,和IsPost用法类似
      

  14.   

    发送的话使用RestSharp直接Add一个Parameter并设置好ParameterType即可