{
    "mode": "perldoc",
    "parameter": "Net::HTTP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AHTTP/json",
    "generated": "2026-06-13T01:07:33Z",
    "synopsis": "use Net::HTTP;\nmy $s = Net::HTTP->new(Host => \"www.perl.com\") || die $@;\n$s->writerequest(GET => \"/\", 'User-Agent' => \"Mozilla/5.0\");\nmy($code, $mess, %h) = $s->readresponseheaders;\nwhile (1) {\nmy $buf;\nmy $n = $s->readentitybody($buf, 1024);\ndie \"read failed: $!\" unless defined $n;\nlast unless $n;\nprint $buf;\n}",
    "sections": {
        "NAME": {
            "content": "Net::HTTP - Low-level HTTP connection (client)\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 6.22\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::HTTP;\nmy $s = Net::HTTP->new(Host => \"www.perl.com\") || die $@;\n$s->writerequest(GET => \"/\", 'User-Agent' => \"Mozilla/5.0\");\nmy($code, $mess, %h) = $s->readresponseheaders;\n\nwhile (1) {\nmy $buf;\nmy $n = $s->readentitybody($buf, 1024);\ndie \"read failed: $!\" unless defined $n;\nlast unless $n;\nprint $buf;\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Net::HTTP\" class is a low-level HTTP client. An instance of the \"Net::HTTP\" class\nrepresents a connection to an HTTP server. The HTTP protocol is described in RFC 2616. The\n\"Net::HTTP\" class supports \"HTTP/1.0\" and \"HTTP/1.1\".\n\n\"Net::HTTP\" is a sub-class of one of \"IO::Socket::IP\" (IPv6+IPv4), \"IO::Socket::INET6\"\n(IPv6+IPv4), or \"IO::Socket::INET\" (IPv4 only). You can mix the methods described below with\nreading and writing from the socket directly. This is not necessary a good idea, unless you know\nwhat you are doing.\n\nThe following methods are provided (in addition to those of \"IO::Socket::INET\"):\n\n\"$s = Net::HTTP->new( %options )\"\nThe \"Net::HTTP\" constructor method takes the same options as \"IO::Socket::INET\"'s as well as\nthese:\n\nHost:            Initial host attribute value\nKeepAlive:       Initial keepalive attribute value\nSendTE:          Initial sendte attributevalue\nHTTPVersion:     Initial httpversion attribute value\nPeerHTTPVersion: Initial peerhttpversion attribute value\nMaxLineLength:   Initial maxlinelength attribute value\nMaxHeaderLines:  Initial maxheaderlines attribute value\n\nThe \"Host\" option is also the default for \"IO::Socket::INET\"'s \"PeerAddr\". The \"PeerPort\"\ndefaults to 80 if not provided. The \"PeerPort\" specification can also be embedded in the\n\"PeerAddr\" by preceding it with a \":\", and closing the IPv6 address on brackets \"[]\" if\nnecessary: \"192.0.2.1:80\",\"[2001:db8::1]:80\",\"any.example.com:80\".\n\nThe \"Listen\" option provided by \"IO::Socket::INET\"'s constructor method is not allowed.\n\nIf unable to connect to the given HTTP server then the constructor returns \"undef\" and $@\ncontains the reason. After a successful connect, a \"Net:HTTP\" object is returned.\n\n\"$s->host\"\nGet/set the default value of the \"Host\" header to send. The $host must not be set to an\nempty string (or \"undef\") for HTTP/1.1.\n\n\"$s->keepalive\"\nGet/set the *keep-alive* value. If this value is TRUE then the request will be sent with\nheaders indicating that the server should try to keep the connection open so that multiple\nrequests can be sent.\n\nThe actual headers set will depend on the value of the \"httpversion\" and\n\"peerhttpversion\" attributes.\n\n\"$s->sendte\"\nGet/set the a value indicating if the request will be sent with a \"TE\" header to indicate\nthe transfer encodings that the server can choose to use. The list of encodings announced as\naccepted by this client depends on availability of the following modules:\n\"Compress::Raw::Zlib\" for *deflate*, and \"IO::Compress::Gunzip\" for *gzip*.\n\n\"$s->httpversion\"\nGet/set the HTTP version number that this client should announce. This value can only be set\nto \"1.0\" or \"1.1\". The default is \"1.1\".\n\n\"$s->peerhttpversion\"\nGet/set the protocol version number of our peer. This value will initially be \"1.0\", but\nwill be updated by a successful readresponseheaders() method call.\n\n\"$s->maxlinelength\"\nGet/set a limit on the length of response line and response header lines. The default is\n8192. A value of 0 means no limit.\n\n\"$s->maxheaderlength\"\nGet/set a limit on the number of header lines that a response can have. The default is 128.\nA value of 0 means no limit.\n\n\"$s->formatrequest($method, $uri, %headers, [$content])\"\nFormat a request message and return it as a string. If the headers do not include a \"Host\"\nheader, then a header is inserted with the value of the \"host\" attribute. Headers like\n\"Connection\" and \"Keep-Alive\" might also be added depending on the status of the\n\"keepalive\" attribute.\n\nIf $content is given (and it is non-empty), then a \"Content-Length\" header is automatically\nadded unless it was already present.\n\n\"$s->writerequest($method, $uri, %headers, [$content])\"\nFormat and send a request message. Arguments are the same as for formatrequest(). Returns\ntrue if successful.\n\n\"$s->formatchunk( $data )\"\nReturns the string to be written for the given chunk of data.\n\n\"$s->writechunk($data)\"\nWill write a new chunk of request entity body data. This method should only be used if the\n\"Transfer-Encoding\" header with a value of \"chunked\" was sent in the request. Note, writing\nzero-length data is a no-op. Use the writechunkeof() method to signal end of entity body\ndata.\n\nReturns true if successful.\n\n\"$s->formatchunkeof( %trailers )\"\nReturns the string to be written for signaling EOF when a \"Transfer-Encoding\" of \"chunked\"\nis used.\n\n\"$s->writechunkeof( %trailers )\"\nWill write eof marker for chunked data and optional trailers. Note that trailers should not\nreally be used unless is was signaled with a \"Trailer\" header.\n\nReturns true if successful.\n\n\"($code, $mess, %headers) = $s->readresponseheaders( %opts )\"\nRead response headers from server and return it. The $code is the 3 digit HTTP status code\n(see HTTP::Status) and $mess is the textual message that came with it. Headers are then\nreturned as key/value pairs. Since key letter casing is not normalized and the same key can\neven occur multiple times, assigning these values directly to a hash is not wise. Only the\n$code is returned if this method is called in scalar context.\n\nAs a side effect this method updates the 'peerhttpversion' attribute.\n\nOptions might be passed in as key/value pairs. There are currently only two options\nsupported; \"laxed\" and \"junkout\".\n\nThe \"laxed\" option will make readresponseheaders() more forgiving towards servers that\nhave not learned how to speak HTTP properly. The \"laxed\" option is a boolean flag, and is\nenabled by passing in a TRUE value. The \"junkout\" option can be used to capture bad header\nlines when \"laxed\" is enabled. The value should be an array reference. Bad header lines will\nbe pushed onto the array.\n\nThe \"laxed\" option must be specified in order to communicate with pre-HTTP/1.0 servers that\ndon't describe the response outcome or the data they send back with a header block. For\nthese servers peerhttpversion is set to \"0.9\" and this method returns (200, \"Assumed OK\").\n\nThe method will raise an exception (die) if the server does not speak proper HTTP or if the\n\"maxlinelength\" or \"maxheaderlength\" limits are reached. If the \"laxed\" option is turned\non and \"maxlinelength\" and \"maxheaderlength\" checks are turned off, then no exception\nwill be raised and this method will always return a response code.\n\n\"$n = $s->readentitybody($buf, $size);\"\nReads chunks of the entity body content. Basically the same interface as for read() and\nsysread(), but the buffer offset argument is not supported yet. This method should only be\ncalled after a successful readresponseheaders() call.\n\nThe return value will be \"undef\" on read errors, 0 on EOF, -1 if no data could be returned\nthis time, otherwise the number of bytes assigned to $buf. The $buf is set to \"\" when the\nreturn value is -1.\n\nYou normally want to retry this call if this function returns either -1 or \"undef\" with $!\nas EINTR or EAGAIN (see Errno). EINTR can happen if the application catches signals and\nEAGAIN can happen if you made the socket non-blocking.\n\nThis method will raise exceptions (die) if the server does not speak proper HTTP. This can\nonly happen when reading chunked data.\n\n\"%headers = $s->gettrailers\"\nAfter readentitybody() has returned 0 to indicate end of the entity body, you might call\nthis method to pick up any trailers.\n\n\"$s->rbuf\"\nGet/set the read buffer content. The readresponseheaders() and readentitybody() methods\nuse an internal buffer which they will look for data before they actually sysread more from\nthe socket itself. If they read too much, the remaining data will be left in this buffer.\n\n\"$s->rbuflength\"\nReturns the number of bytes in the read buffer. This should always be the same as:\n\nlength($s->rbuf)\n\nbut might be more efficient.\n",
            "subsections": []
        },
        "SUBCLASSING": {
            "content": "The readresponseheaders() and readentitybody() will invoke the sysread() method when they\nneed more data. Subclasses might want to override this method to control how reading takes\nplace.\n\nThe object itself is a glob. Subclasses should avoid using hash key names prefixed with \"http\"\nand \"io\".\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "LWP, IO::Socket::INET, Net::HTTP::NB\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Gisle Aas <gisle@activestate.com>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2001 by Gisle Aas.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Net::HTTP - Low-level HTTP connection (client)",
    "flags": [],
    "examples": [],
    "see_also": []
}