Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - have any example code for reading HTTP status codes and headers?

Reply
 
Old 08-31-2007   #1 (permalink)
Hal Rottenberg


 
 

have any example code for reading HTTP status codes and headers?

This is proving to be much less obvious than I'd hoped.

$url="http://blah"
$wc = new-object net.webclient
$html = $wc.DownloadString($url)

Now...I want to get the status code following this donwload. I'm looking for
200, 404, etc. I'm also interested in the headers, e.g. "Server: Apache/2.2.3"
which I've found in WebClient.ResponseHeaders, but that doesn't help with the
status codes. I /think/ it might have to do with a WebException.Status but I
have no idea how to use it. The MSDN example code isn't helping.

http://msdn2.microsoft.com/en-us/lib...on.status.aspx

And would an exception be thrown when the connection is successful? If not, how
would I read the "success codes" (2xx, 3xx) as opposed to the error codes 4xx, 5xx?
--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/

My System SpecsSystem Spec
Old 08-31-2007   #2 (permalink)
Shay Levi


 
 

Re: have any example code for reading HTTP status codes and headers?

Try with xml http

$url = "http://www.cnn.com"
$xHTTP = new-object -com msxml2.xmlhttp;
$xHTTP.open("GET",$url,$false);
$xHTTP.send();
$xHTTP.ResponseText; # returns the html doc like downloadstring
$xHTTP.status # returns the status code

Shay
http://scriptolog.blogspot.com


Quote:

> This is proving to be much less obvious than I'd hoped.
>
> $url="http://blah"
> $wc = new-object net.webclient
> $html = $wc.DownloadString($url)
> Now...I want to get the status code following this donwload. I'm
> looking for 200, 404, etc. I'm also interested in the headers, e.g.
> "Server: Apache/2.2.3" which I've found in WebClient.ResponseHeaders,
> but that doesn't help with the status codes. I /think/ it might have
> to do with a WebException.Status but I have no idea how to use it.
> The MSDN example code isn't helping.
>
> http://msdn2.microsoft.com/en-us/lib...xception.statu
> s.aspx
>
> And would an exception be thrown when the connection is successful?
> If not, how would I read the "success codes" (2xx, 3xx) as opposed to
> the error codes 4xx, 5xx?
>

My System SpecsSystem Spec
Old 08-31-2007   #3 (permalink)
Shay Levi


 
 

Re: have any example code for reading HTTP status codes and headers?

# with a net class

$url = "http://www.cnn.com"
$req=[system.Net.HttpWebRequest]::Create($url);
$res = $req.getresponse();
# $res | gm
$stat = $res.statuscode;

$res.Close();

Pipe $res to gm to see all methods, like get GetResponseHeader()
Shay
http://scriptolog.blogspot.com


Quote:

> Try with xml http
>
> $url = "http://www.cnn.com"
> $xHTTP = new-object -com msxml2.xmlhttp;
> $xHTTP.open("GET",$url,$false);
> $xHTTP.send();
> $xHTTP.ResponseText; # returns the html doc like downloadstring
> $xHTTP.status # returns the status code
> Shay
> http://scriptolog.blogspot.com
Quote:

>> This is proving to be much less obvious than I'd hoped.
>>
>> $url="http://blah"
>> $wc = new-object net.webclient
>> $html = $wc.DownloadString($url)
>> Now...I want to get the status code following this donwload. I'm
>> looking for 200, 404, etc. I'm also interested in the headers, e.g.
>> "Server: Apache/2.2.3" which I've found in WebClient.ResponseHeaders,
>> but that doesn't help with the status codes. I /think/ it might have
>> to do with a WebException.Status but I have no idea how to use it.
>> The MSDN example code isn't helping.
>> http://msdn2.microsoft.com/en-us/lib...exception.stat
>> u s.aspx
>>
>> And would an exception be thrown when the connection is successful?
>> If not, how would I read the "success codes" (2xx, 3xx) as opposed to
>> the error codes 4xx, 5xx?
>>

My System SpecsSystem Spec
Old 08-31-2007   #4 (permalink)
Hal Rottenberg


 
 

Re: have any example code for reading HTTP status codes and headers?

Shay Levi wrote:
Quote:

> Try with xml http
>
> $url = "http://www.cnn.com"
> $xHTTP = new-object -com msxml2.xmlhttp;
Funny, that's exactly how I did it in Vbscript before Powershell came along.
(I'm rewriting an old script.) Maybe it's not worth trying to do it in .NET.

--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/
My System SpecsSystem Spec
Old 08-31-2007   #5 (permalink)
Shay Levi


 
 

Re: have any example code for reading HTTP status codes and headers?

One thing though, the $res.statuscode returns "OK" for insted of numeral
codes.

Shay
http://scriptolog.blogspot.com


Quote:

> # with a net class
>
> $url = "http://www.cnn.com"
> $req=[system.Net.HttpWebRequest]::Create($url);
> $res = $req.getresponse();
> # $res | gm
> $stat = $res.statuscode;
> $res.Close();
>
> Pipe $res to gm to see all methods, like get GetResponseHeader()
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Try with xml http
>>
>> $url = "http://www.cnn.com"
>> $xHTTP = new-object -com msxml2.xmlhttp;
>> $xHTTP.open("GET",$url,$false);
>> $xHTTP.send();
>> $xHTTP.ResponseText; # returns the html doc like downloadstring
>> $xHTTP.status # returns the status code
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> This is proving to be much less obvious than I'd hoped.
>>>
>>> $url="http://blah"
>>> $wc = new-object net.webclient
>>> $html = $wc.DownloadString($url)
>>> Now...I want to get the status code following this donwload. I'm
>>> looking for 200, 404, etc. I'm also interested in the headers, e.g.
>>> "Server: Apache/2.2.3" which I've found in
>>> WebClient.ResponseHeaders,
>>> but that doesn't help with the status codes. I /think/ it might
>>> have
>>> to do with a WebException.Status but I have no idea how to use it.
>>> The MSDN example code isn't helping.
>>> http://msdn2.microsoft.com/en-us/lib...bexception.sta
>>> t
>>> u s.aspx
>>> And would an exception be thrown when the connection is successful?
>>> If not, how would I read the "success codes" (2xx, 3xx) as opposed
>>> to the error codes 4xx, 5xx?
>>>

My System SpecsSystem Spec
Old 08-31-2007   #6 (permalink)
Shay Levi


 
 

Re: have any example code for reading HTTP status codes and headers?

PS C:\Scripts> $res

IsMutuallyAuthenticated : False
Cookies : {}
Headers : {Vary, X-Pad, Keep-Alive, Connection...}
ContentLength : 135531
ContentEncoding :
ContentType : text/html
CharacterSet : ISO-8859-1
Server : Apache
LastModified : 8/31/2007 7:01:46 PM
StatusCode : OK
StatusDescription : OK
ProtocolVersion : 1.1
ResponseUri : http://www.cnn.com/
Method : GET
IsFromCache : False

PS C:\Scripts> $res.Headers
Vary
X-Pad
Keep-Alive
Connection
Accept-Ranges
Content-Length
Cache-Control
Content-Type
Date
Expires
Server

PS C:\Scripts> $res.GetResponseHeader("Content-Length")
135531


Shay
http://scriptolog.blogspot.com


Quote:

> One thing though, the $res.statuscode returns "OK" for insted of
> numeral codes.
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> # with a net class
>>
>> $url = "http://www.cnn.com"
>> $req=[system.Net.HttpWebRequest]::Create($url);
>> $res = $req.getresponse();
>> # $res | gm
>> $stat = $res.statuscode;
>> $res.Close();
>> Pipe $res to gm to see all methods, like get GetResponseHeader()
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> Try with xml http
>>>
>>> $url = "http://www.cnn.com"
>>> $xHTTP = new-object -com msxml2.xmlhttp;
>>> $xHTTP.open("GET",$url,$false);
>>> $xHTTP.send();
>>> $xHTTP.ResponseText; # returns the html doc like downloadstring
>>> $xHTTP.status # returns the status code
>>> Shay
>>> http://scriptolog.blogspot.com
>>>> This is proving to be much less obvious than I'd hoped.
>>>>
>>>> $url="http://blah"
>>>> $wc = new-object net.webclient
>>>> $html = $wc.DownloadString($url)
>>>> Now...I want to get the status code following this donwload. I'm
>>>> looking for 200, 404, etc. I'm also interested in the headers,
>>>> e.g.
>>>> "Server: Apache/2.2.3" which I've found in
>>>> WebClient.ResponseHeaders,
>>>> but that doesn't help with the status codes. I /think/ it might
>>>> have
>>>> to do with a WebException.Status but I have no idea how to use it.
>>>> The MSDN example code isn't helping.
>>>> http://msdn2.microsoft.com/en-us/lib...ebexception.st
>>>> a
>>>> t
>>>> u s.aspx
>>>> And would an exception be thrown when the connection is successful?
>>>> If not, how would I read the "success codes" (2xx, 3xx) as opposed
>>>> to the error codes 4xx, 5xx?

My System SpecsSystem Spec
Old 08-31-2007   #7 (permalink)
Hal Rottenberg


 
 

Re: have any example code for reading HTTP status codes and headers?

Shay Levi wrote:
Quote:

> PS C:\Scripts> $res.GetResponseHeader("Content-Length")
> 135531
Awesome stuff Shay, thanks.


--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/
My System SpecsSystem Spec
Old 08-31-2007   #8 (permalink)
Shay Levi


 
 

Re: have any example code for reading HTTP status codes and headers?

BTW, I wouldn't rely on any of both methods to query responses based on
status codes.
In my scripts I choosed to get the document text (via ResponseText or DownloadString)
and query it for a matching string.
I do it because if the target web server redirects you (which happends in
most cases) to another
page using the Server.Transfer method (e.g., 404 - page not found) than you
wont get 404, you'll
get back "OK" or 200.


Shay
http://scriptolog.blogspot.com


Quote:

> Shay Levi wrote:
>
Quote:

>> PS C:\Scripts> $res.GetResponseHeader("Content-Length") 135531
>>
> Awesome stuff Shay, thanks.
>

My System SpecsSystem Spec
Old 08-31-2007   #9 (permalink)
Hal Rottenberg


 
 

Re: have any example code for reading HTTP status codes and headers?

Shay Levi wrote:
Quote:

> BTW, I wouldn't rely on any of both methods to query responses based on
> status codes.
> In my scripts I choosed to get the document text (via ResponseText or
> DownloadString) and query it for a matching string.
> I do it because if the target web server redirects you (which happends
> in most cases) to another page using the Server.Transfer method (e.g.,
> 404 - page not found) than you wont get 404, you'll get back "OK" or 200.
In my case, I'm actually scanning a set of servers to see which are web servers
and of the lot, which are working based on certain criteria. Or sometimes I
need to spit out a 500 code just because that's what one of the devs wanted, and
I need an easy way to verify that it's working as expected.

I have to say though, I'm liking the COM MsXml method (the old way) much better
than working with .NET so far. I really don't like having to worry about error
trapping. And I think it's pretty difficult to work with $error in general,
it's not giving me what I want. Maybe I'll get used to it...


--

Hal Rottenberg
blog: http://halr9000.com
powershell category:
http://halr9000.com/article/category...ng/powershell/
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
headers is too large in reading panel Live Mail
Too Many Reading Pane Headers Live Mail
Link between Unicode reading font and message headers Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46