Net Http 404 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or error 404 not found posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
404 Error Page
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only http error 404. the requested resource is not found. takes a minute: Sign up How to handle 404 errors with Ruby HTTP::Net? up vote 5 down vote favorite 2 I'm trying to parse web pages but I sometimes get 404 errors. Here's the code I use to get the
404 File Or Directory Not Found
web page: result = Net::HTTP::get URI.parse(URI.escape(url)) How do I test if result is a 404 error code? ruby http share|improve this question edited Nov 18 '14 at 18:19 Nakilon 19.7k86188 asked May 9 '10 at 11:07 Kevin 4911821 add a comment| 2 Answers 2 active oldest votes up vote 10 down vote accepted Rewrite your code like this: uri = URI.parse(url) result = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) } puts result.code puts result.body That will print the status code followed by error 404 google the body. share|improve this answer edited May 9 '10 at 18:42 answered May 9 '10 at 11:12 Theo 98k10103139 Thanks! I tried this but result.status didn't work. Instead, I checked result.class.name == "HTTPOK", it does the trick :-) –Kevin May 9 '10 at 13:14 1 Sorry, it should have been result.code, not result.status. I've updated my answer. –Theo May 9 '10 at 18:43 Note: URI is in module net/http –Veck Hsiao Nov 19 '15 at 8:02 add a comment| up vote 1 down vote As you know, your code will always return the response body, whether there is an error or not. In order to test the response code, use Theo's answer, and the following if statement, for example: if result.code.to_i < 400 puts "success" end This example converts the code (which is a string) to an integer, and treats redirects and various 200 codes as successful. See this for the various codes returned: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes share|improve this answer answered Jul 28 '14 at 6:08 kristianp 2,4901633 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged ruby http or
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Error 401
and policies of this site About Us Learn more about Stack Overflow
400 Error
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http status 404 how to fix it Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it http://stackoverflow.com/questions/2797433/how-to-handle-404-errors-with-ruby-httpnet only takes a minute: Sign up How can I catch a 404? up vote 54 down vote favorite 16 I have the following code: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "HEAD"; request.Credentials = MyCredentialCache; try { request.GetResponse(); } catch { } How can I catch a specific 404 error? The WebExceptionStatus.ProtocolError can only detect that an error occurred, but not give http://stackoverflow.com/questions/1949610/how-can-i-catch-a-404 the exact code of the error. For example: catch (WebException ex) { if (ex.Status != WebExceptionStatus.ProtocolError) { throw ex; } } Is just not useful enough... the protocol exception could be 401, 503, 403, anything really. c# .net exception-handling error-handling http-status-code-404 share|improve this question edited Jan 7 '14 at 19:45 John Saunders 138k20178323 asked Dec 22 '09 at 22:25 JL. 23.7k88246391 11 NNNOOOOOOOOOOOOO! Don't catch System.Exception, and don't depend on the exception text in your handler! –Aaronaught Jan 28 '10 at 16:09 2 So what would you recommend? –Luke101 Jan 28 '10 at 16:11 2 John Saunders' answer was the most complete. I think people just downvoted him out of spite. –Aaronaught Jan 28 '10 at 16:15 2 Don't use throw ex, you'll generate a new exception with an empty call stack. Just use throw. –krbnr Dec 17 '14 at 19:07 add a comment| 11 Answers 11 active oldest votes up vote 74 down vote accepted Use the HttpStatusCode Enumeration, specifically HttpStatusCode.NotFound Something like: HttpWebResponse errorResponse = we.Response as HttpWebResponse; if (errorResponse.StatusCode == HttpStatusCode.NotFound) {
Status codes 301 Moved Permanently 302 Found 303 See Other 403 Forbidden 404 Not Found 451 Unavailable For Legal Reasons v t e The 404 or Not https://en.wikipedia.org/wiki/HTTP_404 Found error message is a Hypertext Transfer Protocol (HTTP) standard response code, in computer network communications, to indicate that the client was able to communicate with a given server, but https://www.stokia.com/support/misc/web-config-custom-httperrors.aspx the server could not find what was requested. The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken error 404 or dead link; hence the 404 error is one of the most recognizable errors encountered on the World Wide Web. Contents 1 Overview 2 Custom error pages 2.1 Tracking/Checking 404 errors 3 Phony 404 errors 4 404 substatus error codes defined by IIS 4.1 Slang usage 5 See also 6 References 7 External links Overview[edit] When communicating via HTTP, a server net http 404 is required to respond to a request, such as a web browser request for a web page, with a numeric response code and an optional, mandatory, or disallowed (based upon the status code) message. In the code 404, the first digit indicates a client error, such as a mistyped Uniform Resource Locator (URL). The following two digits indicate the specific error encountered. HTTP's use of three-digit codes is similar to the use of such codes in earlier protocols such as FTP and NNTP. At the HTTP level, a 404 response code is followed by a human-readable "reason phrase". The HTTP specification suggests the phrase "Not Found"[1] and many web servers by default issue an HTML page that includes both the 404 code and the "Not Found" phrase. A 404 error is often returned when pages have been moved or deleted. In the first case, it is better to employ URL mapping or URL redirection by returning a 301 Moved Permanently response, which can be configured in most server configuration files, or through URL rewriting; in the second case, a 410
your web site. The custom errors can be set or overridden on a site wide or directory-by-directory basis. While some web.config sections require that the directory is set as an application, this isn't one of them. A simple web.config with a httpErrors section may be placed in any directory, and the directory does NOT need to be set as an application. What are http errors? HTTP errors are returned to the client when something goes wrong on the server. Error status codes are returned if the requested file isn't found (404), or due to coding errors in the web page (500), and due to temporary issues such as failed database connections (500). The most common errors are 404 (file not found) and 500 (application) errors. Custom 404 and 500 errors are typically used to provide a friendlier error message to your users. Custom 404 and 500 errors could also redirect the user to the default (or any) page, and are sometimes used to notify the web site administrator of problems on the web site. If you wish to configure custom errors for your site, or even just for a single directory in your site, please follow the directions on this page. 400 Error (bad request) 401 Error (unauthorized) 403 Error (forbidden) 404 Error (not found) 500 Error (internal server error) How it's done Example custom HTTP errors. Comments are enclosed in and are not required. Capture and return specific error types