Recently do the data import, must simulate the HTTP request, calls the framework the corresponding method carries on the data inserting and saves the operation.
The Nodegrass method of Nodejs is used for the corresponding simple imitation.
1, build Nodejs environment.
2. Execute npm install nodegrass command.
3, the introduction of modules, Var ng= require (Nodegrass);
4, the following first look at the Nodegrass bottom of the Get method of the concrete implementation, the code is as follows:
//Get Method Request//Support HTTP and HTTPS request,and Automatic recognition//@Param URL//@Param CallbackNodeGrass.prototype.get =function(Url,callback, Reqheaders, CharSet) {varprotocol =getprotocol (URL); var_defaultcharset = ' UTF8 '; if(typeofCharSet = = = ' String ') {_defaultcharset=CharSet; } if(typeof(reqheaders) = = = "string" && charset = = =undefined) {_defaultcharset=reqheaders; } varNewheader = {}; if(Reqheaders!== undefined &&typeof(reqheaders) = = = "Object") { for(varEleinchreqheaders) {newheader[ele.tolowercase ()]=Reqheaders[ele]; }} newheader["Content-length"] = 0; varOptions ={host:gethost (URL), Port:getport (URL), Path:getpath (URL), method:' GET ', Headers:newheader}; if(protocol = = = HTTP | | protocol = = =HTTPS) { return_sendreq (Protocol,NULL, Options,_defaultcharset,callback); }Else{ Throw"Sorry,this protocol don't Support Now"; }}
As you can see, you just need to add the following parameters to the method call:
URL request Address,
Callback callback function,
Reqheaders Request Header identification, general use ' content-type ': ' application/x-www-form-urlencoded '
CharSet encoding method, generally UTF8
The underlying implementation code for the corresponding post request is as follows:
//Post Method Request//Support HTTP and HTTPS request,and Automatic recognition//@Param URL//@Param Callback//@Param Header//@param postdataNodeGrass.prototype.post =function(url,callback,reqheaders,data,charset) {varprotocol =getprotocol (URL); var_defaultcharset = ' UTF8 '; if(typeofCharSet = = = ' String ') {_defaultcharset=CharSet; } if(typeof(data) = = = ' object ') {data =querystring.stringify (data);} varoptions={host:gethost (URL), Port:getport (URL), Path:getpath (URL), method:' POST ', headers:reqheaders}; if(protocol = = = HTTP | | protocol = = =HTTPS) { return_sendreq (protocol,data,options,_defaultcharset,callback)}Else{ Throw"Sorry,this protocol don't Support Now"; }}
As you can see, you just need to add the following parameters to the method call:
URL request Address,
Callback callback function,
Reqheaders Request Header identification, general use ' content-type ': ' application/x-www-form-urlencoded '
The data request contains specific
CharSet encoding method, generally UTF8
The following is an example of a post-emulation login request, as follows:
varng = require (' Nodegrass ');varReq_headers = { ' Content-type ': ' application/x-www-form-urlencoded '};ng.post ("Http://******/user/login", function(res, status, headers) {if(res.success) {Console.log ("Login successful. "); } Else{Console.log ("Login failed. "); }}, Req_headers, {name:' * * * * *, PWD: ' * * * ', rememberpwd:true}, ' UTF8 '). On (' Error ',function(e) {Console.log ("Got Error:" +e.message); });
This simple example is written according to Nodegrass's API, and if there are deficiencies, please forgive.
Rookie in advance, a little progress every day.
Nodejs Imitation HTTP request Component Nodegrass simple example