HTTP client requests data from other Web sites using the Request method

Source: Internet
Author: User

In node, it is easy to send a request to any Web site and read the response data for that site.

var req=http.request (Options,callback);

Options is a string or an object. If it is a string, it is automatically converted to an object using the Parse method in the URL module. Options can specify the following properties.

Host: The IP address used to specify the domain name or destination host with the default property value "localhost".

Hostname: Used to specify the IP address of the domain name or destination host, the default property is "localhost". If the Hostname property value and the Host property value are specified, the hostname is used preferentially.

Port: Used to specify the port number that the destination server uses for HTTP client connections.

LocalAddress: Used to specify a local interface dedicated to network connections.

Socketpath: Used to specify the destination UNIX domain port.

Method: Used to specify the HTTP request method, the default property value is "GET".

Path: For specifying the path and query string, the default property value is "/".

Headers: Used to specify the client request header object.

Auth: Used to specify the authentication information section. For example: "User:password".

Agent: Used to specify the HTTP proxy. In node, use HTTP. The Agent class represents an HTTP proxy. The so-called HTTP proxy is a browser or proxy server that represents requesting data through HTTP to other websites.

In node, the HTTP proxy defaults to using the Keep-alive connection when requesting data, while using a global HTTP. Agent object to implement all HTTP client requests. The global HTTP is used by default when the Agent property value is not used. The Agent object. You can specify a Http.agent object (that is, a user agent) for the Agent property value display, or you can automatically pick an HTTP that has the current connection state to shut down from the connection pool by specifying the agent property value as false. Agent object (that is, user agent)

1 varHttp=require ("http");2 varoptions={3Hostname: "Www.baidu.com",4Port:80,5Path: "/",6Method: "GET"7 };8 varReq=http.request (Options,function(res) {9Console.log ("Status code:" +res.statuscode);TenConsole.log ("Response header:" +json.stringify (res.headers)); OneRes.setencoding ("UTF8"); ARes.on ("Data",function(chunk) { -Console.log ("Response content:" +chunk); -     }); the }); -Req.end ();

The HTTP client sets timeout and termination requests, and listens for socket port errors with error.

1 varHttp=require ("http");2 varoptions={3Hostname: "Www.baiduuuuuuu.com",//This is a non-existent URL4Port:80,5Path: "/",6Method: "GET"7 };8 varReq=http.request (Options,function(res) {9Console.log ("Status code:" +res.statuscode);TenConsole.log ("Response header:" +json.stringify (res.headers)); OneRes.setencoding ("UTF8"); ARes.on ("Data",function(chunk) { -Console.log ("Response content:" +chunk); -     }); the }); -Req.settimeout (1000,function(){ - Req.abort (); - }); +Req.on ("Error",function(err) { -     if(err.code=== "Econnreset")//Port time-out error +Console.log ("Socket Port timeout:"); A     Else atConsole.log ("Error occurred during request data, error code:" +err.code); - }); -Req.end ();

The Get method sends requests to other Web sites the same way as the request. However, get does not need to call the end () method manually, and node automatically calls end ().

You can make a change to the above code.

1 varHttp=require ("http");2 varoptions={3Hostname: "Www.baiduuuuuuu.com",//This is a non-existent URL4Port:80,5Path: "/",6Method: "GET"7 };8 varReq=http.get (Options,function(res) {9Console.log ("Status code:" +res.statuscode);TenConsole.log ("Response header:" +json.stringify (res.headers)); OneRes.setencoding ("UTF8"); ARes.on ("Data",function(chunk) { -Console.log ("Response content:" +chunk); -     }); the }); -  -Req.settimeout (1000,function(){ - Req.abort (); Console.log ("termination of parental claims"); + }); -Req.on ("Error",function(err) { +     if(err.code=== "Econnreset")//Port time-out error AConsole.log ("Socket Port timeout:"); at     Else -Console.log ("Error occurred during request data, error code:" +err.code); - });

HTTP client requests data from other Web sites using the Request method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.