none
HttpWebRequest 的 IfModifiedSince 怎么设置才等于 0 RRS feed

  • 问题

  • js代码:

    <script type="text/javascript">

      function goPage(n) {
        var qs = 'page=' + n + '&nows=' + (new Date()).getTime();
        getObj('get').innerHTML = '<div class=\'submiting\'>加载中.....</p>';
        var xml = makeXmlReq();
        var url = '/ajax/GetCount.aspx?' + qs;
        xml.onreadystatechange = function () {
          if (xml.readyState == 4) {
            if (xml.status == 200 || xml.status == 304) {
              var txt = xml.responseText;
              getObj('get').innerHTML = txt;
            }
          }
        };
        xml.open('get', url, true);
        xml.setRequestHeader('If-Modified-Since', '0');
        xml.send('');
      }
      goPage(0);
    </script>
    抓包得到的数据:
    GET /ajax/GetCount.aspx?page=0&nows=1367742955902 HTTP/1.1
    Accept: */*
    Accept-Language: zh-cn
    Referer: http://127.0.0.1/index.aspx
    If-Modified-Since: 0
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
    Host: 127.0.0.1
    Connection: Keep-Alive
    Cookie: ASP.NET_SessionId=kzcfqaa3kilah2ejlrkisf45; username=; questionId=

    我想模拟网页里面的 JS 代码 用c# 做 代码如下:

    HttpWebRequest request = WebRequest.Create(" http://127.0.0.1/ajax/GetCount.aspx?id=0&nows=1367742955902") as HttpWebRequest;
    request.Method = "GET";
    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
    request.Accept = "*/*";
    request.Headers("Accept-Language") = "zh-cn";
    request.Headers("Accept-Encoding") = "gzip, deflate";
    request.Referer = " http://127.0.0.1/index.aspx";

    request.Headers.Add("If-Modified-Since", "0");
    //这里会出现错误:System.ArgumentException: 必须使用适当的属性或方法修改“If-Modified-Since”标头。
    //如果写为:request.IfModifiedSince = 这里是 Date类型 不知道怎么写才会等于 0

    //我试了用 request.IfModifiedSince = DateTime.Parse("00:00:00"); 得到的数据包是:If-Modified-Since: Mon, 06 May 2013 16:00:00 GMT要怎么样才可以得到:If-Modified-Since: 0 呢,请大侠们帮帮忙呀

    2013年5月7日 5:57

答案

  • Hi 求真相,

      欢迎来到MSDN中文论坛。

      HttpWebRequest.IfModifiedSince 不能设置为0,因为这个是一个修改的起点时间值,可以是当前时间前的某个时间点。

     
      DateTime targetDate = DateTime.Now;
        // Set a target date of a week ago
        targetDate.AddDays(-7.0);
        myHttpWebRequest.IfModifiedSince = targetDate;
    
    


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    2013年5月8日 4:42
    版主