Posted By: sbc | Mar 7th, 2006 @ 3:46 AM
page 1 of 1
Comments: 9 | Views: 12036
sbc
sbc
GW R/Me
I am attempting to get the response headers (specifically the Last-Modified one). However, all I get is Connection and Host.
WebRequest request = WebRequest.Create("http://channel9.msdn.com/rss.aspx?forumid=15);
request.Method = "HEAD";
try
{
    request.GetResponse();
    Response.Write(request.Headers);
    Response.End();
}
catch(WebException ex)
{
    Response.Write(ex.ToString());
}

Results in:

Connection: Keep-Alive Host: channel9.msdn.com
Maurits
Maurits
AKA Matthew van Eerde
WebRequest req = ...
WebResponse resp = req.GetResponse()

foo(resp.Headers)
Maurits
Maurits
AKA Matthew van Eerde
Yes, req.Headers is the headers sent to the remote server

I imagine you could do something like

req.Headers.Item("User-Agent") = "SBCBot/1.0";

to change the user agent.

WebRequest objects don't use a cache... you'll have to build your own.  You should set "If-Modified-Since" on the request -- if you get back a "Not Modifed" response instead of data, the data wasn't modified.

Make sure your If-Modified-Since header is an HTTP-standard date format.

EDIT: I'm guessing, though, that Channel 9's RSS feed doesn't honor the If-Modified-Since header.
Maurits
Maurits
AKA Matthew van Eerde
WebRequest objects have an IfModifiedSince property that handles setting the If-Modified-Since header for you.

EDIT: BTW, the example on that page makes absolutely no sense to me.
EDIT2: There's a UserAgent property too.  Here's the list of HttpWebRequest.Headers properties.
EDIT3: And here's the HttpWebResponse.Headers properties.  Note LastModified is there.

EDIT4:
So you can do...

req.UserAgent = strIEUserAgent;
req.IfModifiedSince = dateLastModificationIKnowAbout;

...

dateLastModificationIKnowAbout = resp.LastModified;

EDIT5:

For completeness...

Suppose you set IfModifiedSince.

If the content has not been modified, the server should send back a response with Status Code 304 Not Modified.

Otherwise you can assume either Angel the content was modified or (b) the server doesn't support If-Modified-Since for that page.

EDIT6:

To wit:

if (resp.StatusCode = HttpStatusCode.NotModified)
{
    // no changes since If-Modified-Since
}
else
{
    // new content... pull from resp
}

EDIT7: Emailed a proposed new example skeleton to the feedback link on the page referenced in the first edit
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
sbc wrote:
Also, has it ever been updated since .NET 1.1 came out?

Yes, there was the October 2003 update for the .Net 1.1 SDK and MSDN for VS2003.
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
sbc wrote:
Sven Groot wrote:
sbc wrote: Also, has it ever been updated since .NET 1.1 came out?

Yes, there was the October 2003 update for the .Net 1.1 SDK and MSDN for VS2003.

What was in the update?

According to the download page:
"
  • Over 5,000 additional code examples for Visual Basic, Visual C#, and Visual C++.
  • Bug fixes.
  • Content improvements and clarifications.
    "