zoukankan      html  css  js  c++  java
  • NodeJS创建http client

     1 var http = require('http');
     2 
     3 var data = JSON.stringify({ 'important': 'data' });
     4 var cookie = 'something=anything'
     5 
     6 var client = http.createClient(80, 'www.example.com');
     7 
     8 var headers = {
     9     'Host': 'www.example.com',
    10     'Cookie': cookie,
    11     'Content-Type': 'application/json',
    12     'Content-Length': Buffer.byteLength(data,'utf8')
    13 };
    14 
    15 var request = client.request('POST', '/', headers);
    16 
    17 // listening to the response is optional, I suppose
    18 request.on('response', function(response) {
    19   response.on('data', function(chunk) {
    20     // do what you do
    21   });
    22   response.on('end', function() {
    23     // do what you do
    24   });
    25 });
    26 // you'd also want to listen for errors in production
    27 
    28 request.write(data);
    29 
    30 request.end();
  • 相关阅读:
    删除指定字符
    Palindromes _easy version
    统计元音
    查找最大元素
    首字母变大写
    Intent加强
    GUI_键盘事件
    GUI_鼠标事件
    GUI_事件监听机制与ActionListener演示
    GUI概述与Frame演示
  • 原文地址:https://www.cnblogs.com/yimu/p/2756934.html
Copyright © 2011-2022 走看看