开发者

HTTP Response changed?

开发者 https://www.devze.com 2023-03-20 19:20 出处:网络
i have an HttpWebRequest that do \'POST\' to a web server and get an HTML page In response. I\'ve been asked how is the best practice to know that the response i got has been changed or not?

i have an HttpWebRequest that do 'POST' to a web server and get an HTML page In response.

I've been asked how is the best practice to know that the response i got has been changed or not?

I can't relay on the web server headers, they don't have to be.

this will increase performance in a way that i wont be need to parse the res开发者_运维百科ponse over again and will go to the next request in a half of a sec or so.

thank you in advanced


You can tell your webserver about last modification date. see here. If you can't rely on that you have to parse your response anyway. You could do that quickly using md5. So you "md5" your current response and compare it with the previous.


You shouldn't be attempting to rely on the headers for a POST request, as it shouldn't emit any caching headers anyway.

What you need to do instead is perform a hash/checksum (this can either be CRC(32) for absolute performance or a "real" hash such as md5) on the returned content (this means, everything underneath the \r\n\r\n in the headers) and do a comparison that way.

It should be good enough to store the last request's checksum/hash and compare against that.

For example (psuedo):

int lastChecksum = 0;
bool hasChanged() {
    performWebRequest();
    string content = stripHeaders();
    int checksum = crc32string(content);
    if(checksum != lastChecksum) {
        lastChecksum = checksum;
        return true;
    }
    return false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消