在Node.js中实现HTTPS客户端





/**
 * nodejsで任意のサーバーにHTTPSでリクエストを行う
 *
 *
 */


// 各種モジュールの読み込み
var http = require("https");
var fs = require("fs");
var server= http.createServer();
var webclient = require("request");

webclient.post({
  url: "https://sample.com/index/login",
  headers: {
    "Access-Token": "senbiki_test"
  },
  body: JSON.stringify({foo: "bar"})
}, function (error, response, body){
  console.log(body);
});

var  options = {
    host: "your-origin.com",
    port: 443,
    method: "POST",
    path: "/api/something/v1/authenticate/login",
    headers: {
        "Access-Token": "my-uesr-test"
    }
};


var accessTokenSecret = null;
var request = http.request(options, function (response) {
    console.dir("~~~~")
    console.dir(response);
    for (var prop in response)
    {
        console.dir(prop)
    }
    console.dir(response.statusCode);
    console.dir(JSON.stringify(response.headers.Response.access_token_secret))
    response.setEncoding("UTF-8");
    response.on("data", function (chunk)
    {
        console.dir("Response: " + chunk);
    })

    response.on("end", function (chunk)
    {
        console.dir("Response End");
    })
})




request.write("");
request.on("error", function (e)
{
    console.dir("~~~~~~~");
    console.dir(e);
    console.dir("error: " + e.message)
})
request.end();

options = {
    host: "sample.com",
    port: 443,
    path: "",
    method: "get",
    headers: {
        "Access-Token-Secret": "任意のアクセストークン"
    }
}


server.on("request", function (request, response)
{
    // console.dir(request);
    // console.dir(response);
    response.writeHead(200, { "Content-Type": "text/html;charset=UTF-8" });
    response.write("マルチバイト文字列");
    response.end();
    var f = fs.readFileSync("./index.js", {encoding: "UTF-8"});
    // console.dir(f);
})
server.listen(3000);

广告
将在 10 秒后关闭
bannerAds