• TypeScript之Https通信


    NetWorkRequest.ts(源代码如下)

    import * as https from "https";
    import * as vscode from 'vscode';
    import * as querystring from 'querystring';
    export class NetWorkRequest {
    
        public static sendRequest(userCode: string) {
    
            vscode.window.showInformationMessage("userCode:" + userCode);
    
            var userId = userCode;
            var post_data = { userId: userId }
            var contents = querystring.stringify(post_data);
    
            var options = {
                hostname: "www.test.com",
                port: 443, //443
                path: "/test-web/api/sysUser/getUserCodeByInfo?" + contents,
                method: "POST",
                rejectUnauthorized: false,
                headers: {
                    Accept: "*/*",
                    "Accept-Encoding": "utf-8",
                    "Accept-Language": "zh-CN,zh;q=0.8",
                    Connection: "keep-alive",
                    Host: "www.test.com"
                },
    
            };
            var mData = "";
            var req = https.request(options, function (res) {
                res.setEncoding('utf-8');
                res.on("data", function (d) {
    
                    var data = JSON.parse(mData+d);
                    console.log("============================================data======================================================:" + data);
                   
                });
    
            });
            // req.write(contents);
            req.on("error", function (e) {
    
            });
            req.end();
        }
    
    }

    那么如何调用呢?

    调用其实与Java调用很相似,基本上都是类名.方法。

    如下调用:

    import { NetWorkRequest } from './NetWorkRequest';
    NetWorkRequest.sendRequest(userCode);
  • 相关阅读:
    servlet中Cookie的编码问题
    servlet关于转发用法
    eclipse界面更改为黑色
    猜字符小游戏——可以设置等级
    uva 10655
    uva11029
    UVA11388 GCD LCM1 2 -1
    uva 11609
    poj 2436
    poj 2431
  • 原文地址:https://www.cnblogs.com/youcong/p/11070359.html
Copyright © 2020-2023  润新知