- MODULE Net::HTTPExceptions
- MODULE Net::HTTPHeader
- MODULE Net::SMTPError
- CLASS Net::APOP
- CLASS Net::APOPSession
- CLASS Net::FTP
- CLASS Net::FTPConnectionError
- CLASS Net::FTPError
- CLASS Net::FTPPermError
- CLASS Net::FTPProtoError
- CLASS Net::FTPReplyError
- CLASS Net::FTPTempError
- CLASS Net::HTTP
- CLASS Net::HTTPAccepted
- CLASS Net::HTTPBadGateway
- CLASS Net::HTTPBadRequest
- CLASS Net::HTTPBadResponse
- CLASS Net::HTTPClientError
- CLASS Net::HTTPConflict
- CLASS Net::HTTPContinue
- CLASS Net::HTTPCreated
- CLASS Net::HTTPError
- CLASS Net::HTTPExpectationFailed
- CLASS Net::HTTPFatalError
- CLASS Net::HTTPForbidden
- CLASS Net::HTTPFound
- CLASS Net::HTTPGatewayTimeOut
- CLASS Net::HTTPGenericRequest
- CLASS Net::HTTPGone
- CLASS Net::HTTPHeaderSyntaxError
- CLASS Net::HTTPInformation
- CLASS Net::HTTPInternalServerError
- CLASS Net::HTTPLengthRequired
- CLASS Net::HTTPMethodNotAllowed
- CLASS Net::HTTPMovedPermanently
- CLASS Net::HTTPMovedTemporarily
- CLASS Net::HTTPMultipleChoice
- CLASS Net::HTTPNoContent
- CLASS Net::HTTPNonAuthoritativeInformation
- CLASS Net::HTTPNotAcceptable
- CLASS Net::HTTPNotFound
- CLASS Net::HTTPNotImplemented
- CLASS Net::HTTPNotModified
- CLASS Net::HTTPOK
- CLASS Net::HTTPPartialContent
- CLASS Net::HTTPPaymentRequired
- CLASS Net::HTTPPreconditionFailed
- CLASS Net::HTTPProxyAuthenticationRequired
- CLASS Net::HTTPRedirection
- CLASS Net::HTTPRequest
- CLASS Net::HTTPRequestEntityTooLarge
- CLASS Net::HTTPRequestTimeOut
- CLASS Net::HTTPRequestURITooLarge
- CLASS Net::HTTPRequestURITooLong
- CLASS Net::HTTPRequestedRangeNotSatisfiable
- CLASS Net::HTTPResetContent
- CLASS Net::HTTPResponse
- CLASS Net::HTTPRetriableError
- CLASS Net::HTTPSeeOther
- CLASS Net::HTTPServerError
- CLASS Net::HTTPServerException
- CLASS Net::HTTPServiceUnavailable
- CLASS Net::HTTPSession
- CLASS Net::HTTPSuccess
- CLASS Net::HTTPSwitchProtocol
- CLASS Net::HTTPTemporaryRedirect
- CLASS Net::HTTPUnauthorized
- CLASS Net::HTTPUnknownResponse
- CLASS Net::HTTPUnsupportedMediaType
- CLASS Net::HTTPUseProxy
- CLASS Net::HTTPVersionNotSupported
- CLASS Net::IMAP
- CLASS Net::POP
- CLASS Net::POP3
- CLASS Net::POP3Session
- CLASS Net::POPAuthenticationError
- CLASS Net::POPBadResponse
- CLASS Net::POPError
- CLASS Net::POPMail
- CLASS Net::POPSession
- CLASS Net::SMTP
- CLASS Net::SMTPAuthenticationError
- CLASS Net::SMTPFatalError
- CLASS Net::SMTPServerBusy
- CLASS Net::SMTPSession
- CLASS Net::SMTPSyntaxError
- CLASS Net::SMTPUnknownError
- CLASS Net::SMTPUnsupportedCommand
- CLASS Net::Telnet
HTTPSession | = | HTTP |
An HTTP client API for Ruby.Net::HTTP provides a rich library which can be used to build HTTP user-agents. For more details about HTTP see [RFC2616](www.ietf.org/rfc/rfc2616.txt) Net::HTTP is designed to work closely with URI. URI::Generic#host, URI::Generic#port and URI::HTTP#request_uri are designed to work with Net::HTTP. If you are only performing a few GET requests you should try OpenURI. Simple ExamplesAll examples assume you have loaded Net::HTTP with:
This will also require 'uri' so you don't need to require it separately. The Net::HTTP methods in the following section do not persist connections. They are not recommended if you are performing many HTTP requests. GET
GET by URI
GET with Dynamic Parameters
POST
POST with Multiple Values
How to use Net::HTTPThe following example code can be used as the basis of a HTTP user-agent which can perform a variety of request types using persistent connections.
Net::HTTP.start immediately creates a connection to an HTTP server which is kept open for the duration of the block. The connection will remain open for multiple requests in the block if the server indicates it supports persistent connections. The request types Net::HTTP supports are listed below in the section “HTTP Request Classes”. If you wish to re-use a connection across multiple HTTP requests without automatically closing it you can use ::new instead of ::start. request will automatically open a connection to the server if one is not currently open. You can manually close the connection with finish. Response Data
Following RedirectionEach Net::HTTPResponse object belongs to a class for its response code. For example, all 2XX responses are instances of a Net::HTTPSuccess subclass, a 3XX response is an instance of a Net::HTTPRedirection subclass and a 200 response is an instance of the Net::HTTPOK class. For details of response classes, see the section “HTTP Response Classes” below. Using a case statement you can handle various types of responses properly:
POSTA POST can be made using the Net::HTTP::Post request class. This example creates a urlencoded POST body:
At this time Net::HTTP does not support multipart/form-data. To send multipart/form-data use Net::HTTPGenericRequest#body= and Net::HTTPHeader#content_type=:
Other requests that can contain a body such as PUT can be created in the same way using the corresponding request class (Net::HTTP::Put). Setting HeadersThe following example performs a conditional GET using the If-Modified-Since header. If the files has not been modified since the time in the header a Not Modified response will be returned. See RFC 2616 section 9.3 for further details.
Basic AuthenticationBasic authentication is performed according to [RFC2617](www.ietf.org/rfc/rfc2617.txt)
Streaming Response BodiesBy default Net::HTTP reads an entire response into memory. If you are handling large files or wish to implement a progress bar you can instead stream the body directly to an IO.
HTTPSHTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
In previous versions of ruby you would need to require 'net/https' to use HTTPS. This is no longer true. ProxiesNet::HTTP::Proxy has the same methods as Net::HTTP but its instances always connect via the proxy instead of directly to the given host.
Net::HTTP::Proxy returns a Net::HTTP instance when proxy_addr is nil so there is no need for conditional code. See Net::HTTP::Proxy for further details and examples such as proxies that require a username and password. HTTP Request ClassesHere is the HTTP request class hierarchy. HTTP Response ClassesHere is HTTP response class hierarchy. All classes are defined in Net module and are subclasses of Net::HTTPResponse.
There is also the Net::HTTPBadResponse exception which is raised when there is a protocol error. |
||
POP | = | POP3 |
Net::POP3What is This Library?This library provides functionality for retrieving email via POP3, the Post Office Protocol version 3. For details of POP3, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt). ExamplesRetrieving MessagesThis example retrieves messages from the server and deletes them on the server. Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your POP3 server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.
Shortened CodeThe example above is very verbose. You can shorten the code by using some utility methods. First, the block form of Net::POP3.start can be used instead of Net::POP3.new, Net::POP3#start and Net::POP3#finish.
Net::POP3#delete_all is an alternative for each_mail and delete.
And here is an even shorter example.
Memory Space IssuesAll the examples above get each message as one big string. This example avoids this.
Using APOPThe net/pop library supports APOP authentication. To use APOP, use the Net::APOP class instead of the Net::POP3 class. You can use the utility method, Net::POP3.APOP(). For example:
Fetch Only Selected Mail Using 'UIDL' POP CommandIf your POP server provides UIDL functionality, you can grab only selected mails from the POP server. e.g.
The Net::POPMail#unique_id method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message. |
||
POPSession | = | POP3 |
Net::POP3What is This Library?This library provides functionality for retrieving email via POP3, the Post Office Protocol version 3. For details of POP3, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt). ExamplesRetrieving MessagesThis example retrieves messages from the server and deletes them on the server. Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your POP3 server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.
Shortened CodeThe example above is very verbose. You can shorten the code by using some utility methods. First, the block form of Net::POP3.start can be used instead of Net::POP3.new, Net::POP3#start and Net::POP3#finish.
Net::POP3#delete_all is an alternative for each_mail and delete.
And here is an even shorter example.
Memory Space IssuesAll the examples above get each message as one big string. This example avoids this.
Using APOPThe net/pop library supports APOP authentication. To use APOP, use the Net::APOP class instead of the Net::POP3 class. You can use the utility method, Net::POP3.APOP(). For example:
Fetch Only Selected Mail Using 'UIDL' POP CommandIf your POP server provides UIDL functionality, you can grab only selected mails from the POP server. e.g.
The Net::POPMail#unique_id method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message. |
||
POP3Session | = | POP3 |
Net::POP3What is This Library?This library provides functionality for retrieving email via POP3, the Post Office Protocol version 3. For details of POP3, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt). ExamplesRetrieving MessagesThis example retrieves messages from the server and deletes them on the server. Messages are written to files named 'inbox/1', 'inbox/2', .… Replace 'pop.example.com' with your POP3 server address, and 'YourAccount' and 'YourPassword' with the appropriate account details.
Shortened CodeThe example above is very verbose. You can shorten the code by using some utility methods. First, the block form of Net::POP3.start can be used instead of Net::POP3.new, Net::POP3#start and Net::POP3#finish.
Net::POP3#delete_all is an alternative for each_mail and delete.
And here is an even shorter example.
Memory Space IssuesAll the examples above get each message as one big string. This example avoids this.
Using APOPThe net/pop library supports APOP authentication. To use APOP, use the Net::APOP class instead of the Net::POP3 class. You can use the utility method, Net::POP3.APOP(). For example:
Fetch Only Selected Mail Using 'UIDL' POP CommandIf your POP server provides UIDL functionality, you can grab only selected mails from the POP server. e.g.
The Net::POPMail#unique_id method returns the unique-id of the message as a String. Normally the unique-id is a hash of the message. |
||
APOPSession | = | APOP |
This class is equivalent to POP3, except that it uses APOP authentication. |
||
SMTPSession | = | SMTP |
Net::SMTPWhat is This Library?This library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. For details of SMTP itself, see [RFC2821] (www.ietf.org/rfc/rfc2821.txt). What is This Library NOT?This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail or search for alternatives in RubyGems.org or The Ruby Toolbox. FYI: the official documentation on internet mail is: [RFC2822] (www.ietf.org/rfc/rfc2822.txt). ExamplesSending MessagesYou must open a connection to an SMTP server before sending messages. The first argument is the address of your SMTP server, and the second argument is the port number. Using Net::SMTP.start with a block is the simplest way to do this. This way, the SMTP connection is closed automatically after the block is executed.
Replace 'your.smtp.server' with your SMTP server. Normally your system manager or internet provider supplies a server for you. Then you can send messages.
Closing the SessionYou MUST close the SMTP session after sending messages, by calling the finish method:
You can also use the block form of Net::SMTP.start/SMTP#start. This closes the SMTP session automatically:
I strongly recommend this scheme. This form is simpler and more robust. HELO domainIn almost all situations, you must provide a third argument to Net::SMTP.start/SMTP#start. This is the domain name which you are on (the host to send mail from). It is called the “HELO domain”. The SMTP server will judge whether it should send or reject the SMTP session by inspecting the HELO domain.
SMTP AuthenticationThe Net::SMTP class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554]) To use SMTP authentication, pass extra arguments to Net::SMTP.start/SMTP#start.
|