![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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; (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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | Re: have any example code for reading HTTP status codes and headers? Shay Levi wrote: Quote: > PS C:\Scripts> $res.GetResponseHeader("Content-Length") > 135531 -- Hal Rottenberg blog: http://halr9000.com powershell category: http://halr9000.com/article/category...ng/powershell/ |
My System Specs![]() |
| | #8 (permalink) |
| | 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 >> > |
My System Specs![]() |
| | #9 (permalink) |
| | 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. 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 Specs![]() |
![]() |
| 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 | |||