zoukankan      html  css  js  c++  java
  • HTTP客户端/服务端 POST/GET

    获取GET请求内容

    实例 

    //引入模块
    var http=require('http');
    var urls=require('url');
    var util=require('util');
    //创建服务
    http.createServer(function(req,res){
     res.writeHead(200,{'Content-Type':'text/plain'});
     // 解析 url 参数
        var params = url.parse(req.url, true).query;
        res.write("网站名:" + params.name);
        res.write(" ");
        res.write("网站 URL:" + params.url);
        res.end();


    }).listen(3001);

     //下面的列子是 在nodejs下 创建客户端和服务端  

    知识点:http模块提供了两个函数http.request(post请求)和http.get(get请求),功能是作为客户端向HTTP服务器发起请求

      
                1.http.request(options,callback)发起HTTP请求,接受两个参数,option是一个类似关联数组的对象,表示请求的参数,callback是请求的回调函数,option常用的参数如下
                    host:请求网站的域名或IP地址
                    port:请求网站的端口,默认是80,
                    method:请求方法,模式是GET/POST
                    path:请求的相对于根的路径,默认是"/"。QueryString应该包含在其中,例如/search?query=marico
                    headers:一个关联数组对象,为请求头的内容
                    callback传递一个参数,为http.ClientResponse的实例
     
                 2.http.get(options,callback) http模块还提供了一个更加简便的方法用于处理GET请求:http.get。它是http.request的简化版,唯一的区别在于http.get自动将请求方法设为GET请求,同时不需要手动调用req.end();
     

    var http=require('http');
    var url=require('url');
    var util=require('util');
    //启动服务
    http.createServer(function(req,res){
     console.log('请求到来,解析参数');
     var params=url.parse(req.url,true);
     console.log('解析完成');
     console.log(util.inspect(params));//得到是具体的url信息
     console.log('向客户端返回');
     res.end(params.query.name);//res.write(params.query.name); 这边是向客户端发生回复信息  触发客户端的data事件得到data数据
    }).listen(3000);

    //客户端请求
    var request=http.get({
     host:'localhost',
     path:'/user?name=marico&age=21',
     port:3000},function(res){
     res.setEncoding('utf-8');
     res.on('data',function(data){
      console.log(' 服务端相应回来的数据为:'+data);
     })
    });

    获取 POST 请求内容 

    实例

    //引入两个模块 http querystring
    var http = require('http');
    var querystring = require('querystring');

    var postHTML =
      '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
      '<body>' +
      '<form method="post">' +
      '网站名: <input name="name"><br>' +
      '网站 URL: <input name="url"><br>' +
      '<input type="submit">' +
      '</form>' +
      '</body></html>';
     
    //创建服务器
    http.createServer(function (req, res) {
      var body = "";
    //将post请求的数据存储到body上
      req.on('data', function (chunk) {
        body += chunk;
      });
      req.on('end', function () {
       
        // 解析参数
        body = querystring.parse(body);
        //parse before:body->xx=11&yy=22形式    parse after body:{"xx":"11","yy":"22"}
        // 设置响应头部信息及编码
        res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});

        if(body.name && body.url) { // 输出提交的数据
            res.write("网站名:" + body.name);
            res.write("<br>");
            res.write("网站 URL:" + body.url);
        } else {  // 输出表单
            res.write(postHTML);
        }
        res.end();
      });
    }).listen(3000);
     
     
    //下面的列子是nodejs 客户端服务端

    var http=require('http');
    var querystring=require('querystring');
    //启动服务
    http.createServer(function(req,res){
     console.log('请求到来,解析参数');
     //解析post请求
     var post='';
     req.on('data',function(chunk){
      post+=chunk;
     });
     req.on('end',function(){
      post=querystring.parse(post);
      //解析完成
      console.log('参数解析完成,返回name参数');
      res.end(post.name);
     });
    }).listen(3000);


    //客户端请求
    var contents=querystring.stringify({
     name:'marico',
     age:21,
     address:'beijing'
    });
    //Ext.encode();
    //声明请求参数
    var options={
     host:'localhost',
     path:'/',
     port:3000,
     method:'POST',
     headers:{
      'Content-Type':'application/x-www-form-urlencoded',
      'Content-Length':contents.length
     }
    };
    //发送请求
    var req=http.request(options,function(res){
     res.setEncoding('utf-8');
     res.on('data',function(data){ //等待服务端返回数据触发函数
      console.log('后台返回数据');
      console.log(data);
     })
    });
    req.write(contents);
    //必须调用end()
    req.end();

  • 相关阅读:
    How to extend MySQLInnoDBDialect?
    Hibernate Session
    org/apache/xerces/xni/parser/XMLConfigurationException
    Hibernate.xml
    Oracle自带的sql developer导入导出数据 java程序员
    c#的DateTime.Now函数详解 java程序员
    [转]随着个性化数据带来的价值,为什么不销售你自己的数据?惠普实验室告诉你如何完成 java程序员
    [原]怎样在Eclipse中看到Android源码API java程序员
    HTML5的未来 HTML5 还能走多远? java程序员
    帮助你开发基于HTML5的网站原型页面 HTML5 Bones java程序员
  • 原文地址:https://www.cnblogs.com/IT-LM/p/6985529.html
Copyright © 2011-2022 走看看