A simple example of using httpclient post data to implement a login Nodejs service interface
- Nodejs: Using EXPRESSJS + Body-parser to develop a simple login login service Interface
var express = require (' Express '), Bodyparser = require (' Body-parser '); var app = Express (); App.use (Bodyparser.json ( {limit: ' 1MB '}); App.use (bodyparser.urlencoded ({extended:true, limit: ' 1MB '));//Login Interface > Http://127.0.0.1:3000/loginapp.post ('/ Login ', function (req,res) { var username = req.body.username; var password = Req.body.password; Console.log (' username: ' + username + '; password: "+password); Res.json ({"Message": ' Welcome to login ', ' status ': 19999});}); App.listen (, function () {Console.log (' server is running ');});
- Create a new WPF project, add a button to the main form, and then write the button's Click event
Private void _btnpost_click_1 (object sender, RoutedEventArgs e) { New HttpClient ();
var postdata New string , string >> ();
PostData. ADD (Newkeyvaluepair<string,string> ("username","Vison")); PostData. ADD (Newkeyvaluepair<string,string> ("Password","vison1987")); Httpcontent content=Newformurlencodedcontent (postdata); Client. Postasync ("Http://127.0.0.1:3000/login", content). ContinueWith ((posttask) ={posttask. Result.Content.ReadAsStringAsync (). ContinueWith (Result)={MessageBox.Show (result. Result.tostring ()); =>s output JSON format string{"Message": ' Welcome to login ', ' status ': 19999}
});
}
=+ test Complete.
Data interaction with the Nodejs API interface via HttpClient