1.0.38 • Published 10 months ago

http-clientp v1.0.38

Weekly downloads
104
License
MIT
Repository
github
Last release
10 months ago

http-clientp

  • 支持基于http、https、proxy的请求
  • 支持回调函数附带数据
  • 支持设置超时时间
  • 支持使用函数返回代理
  • 具体用法见test/test.js
  • 运行test请先安装mocha
  • 运行test请启动本地8888端口的代理,fiddler或者charles或者其他代理都可以

httpcontext模板格式:

{
	"request":{
		"proxy":{"host":"127.0.0.1","port":8888},//设置请求代理,共两种设置方式,任选其一即可
		"http_proxy":"http://127.0.0.1:8888",    //设置请求代理,共两种设置方式,任选其一即可
		"headers":{},		
		"encode":true,   //是否对url进行encodeURI操作,默认为true
		"timeout":"1000", //请求超时
		"postdata":"a=1&b=1"    //post请求参数,
		 options: { 
                    method: 'GET',
                    path: 'https://www.npmjs.com/package/http-clientp' 
                   }
	},
	"response":{
		"charset":"",//返回编码
		"statusCode":200
	},
	"data":{             //附带数据
	    "key":"value",
	    "key1":"value1"
	}
}

  • 基础用法
var options = {
    "method":"GET",
    "path":"https://www.baidu.com/"
};
httpClient.request(options, function (err, body, res, httpContext) {
    console.log(res.statusCode)
});
****GET
var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': 'https://www.baidu.com/'}, null, {'host': '127.0.0.1', 'port': 8888}, 1000);
httpClient.request(httpcontext.request.options, function (err, body, res, httpContext) {
    console.log(res.statusCode);
    console.log(httpContext.data.key);
    console.log(httpContext.data.url);
}, httpcontext);
****POST
var httpcontext = httpClient.build_httpcontext('POST', {
    'key': 'value',
    'url': test_url
}, 'a=1&b=2&c=3', {'host': '127.0.0.1', 'port': 8888}, 1000);
httpClient.request(httpcontext.request.options, function (err, body, res, httpContext) {
    console.log(res.statusCode);
    console.log(httpContext.data.key);
    console.log(httpContext.data.url);
}, httpcontext);
  • get请求
 httpClient.get('https://www.baidu.com/', function (err, body, res, httpContext) {
    console.log(res.statusCode)
 });
  • 超时请求
    var httpcontext = {'request': {'timeout': 1000}};
    httpClient.get(test_url, function (err, body, res, httpContext) {
        console.log(res.statusCode)
    }, httpcontext);
  • 代理请求
    var httpcontext = {'request': {'proxy': {'host': '127.0.0.1', 'port': 8888}}};
    httpClient.get(test_url, function (err, body, res, httpContext) {
        console.log(res.statusCode)
    }, httpcontext);
  • 代理请求,http_proxy方式
    var httpcontext = {'request': {'http_proxy': 'http://127.0.0.1:8888'}};
    httpClient.get(test_url, function (err, body, res, httpContext) {
        console.log(res.statusCode)
    }, httpcontext);
  • 代理请求,http_proxy方式,支持proxy auth
    var httpcontext = {'request': {'http_proxy': 'http://username:password@127.0.0.1:8888'}};
    httpClient.get(test_url, function (err, body, res, httpContext) {
        console.log(res.statusCode)
    }, httpcontext);
  • 将请求的数据附带到结果中
    var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'city':'北京'});
    httpClient.get(test_url, function (err, body, res, httpContext) {
        console.log(res.statusCode)
        console.log(httpContext.data.key)
        console.log(httpContext.data.city)
    }, httpcontext);
  • 根据同步函数设置代理
    var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': test_url});
    httpcontext.proxymodel = 'dynamic';
    httpcontext.callback = function (httpcontext) {
        console.log(httpcontext.res.statusCode)
    };
    httpClient.request_select_proxy(httpcontext, function (callback) {
        var http_proxy = syncProxy();//根据函数获取代理,代理是动态获取的时候可以用此种方法
        httpcontext.request.http_proxy = http_proxy;
        callback(httpcontext);
    });
    function syncProxy() {
        return '127.0.0.1:8888';
    }
  • 根据异步函数设置代理
    var httpcontext = httpClient.build_httpcontext('GET', {'key': 'value', 'url': test_url});
    httpcontext.proxymodel = 'dynamic';
    httpcontext.callback = function (httpcontext) {
        console.log(httpcontext.res.statusCode)
    };
    httpClient.request_select_proxy(httpcontext, function (callback) {
        asyncProxy(function (http_proxy) {
            httpcontext.request.http_proxy = http_proxy;
            callback(httpcontext);
        })
    });
    function asyncProxy(calback) {
        calback(proxy_url);
    }
  • 设置头
    var httpContext = {'request': {'headers': {"Cookie": "a=a"}}};
    httpClient.get({'path': test_url, 'headers': {"Cookie": "b=b"}}, function (err, body, res, httpContext) {
        console.log(httpContext.request.headers.Cookie)
        console.log(res.statusCode)
    }, httpContext);
    var httpClient = require('../lib/http-clientp');
    var fs = require('fs');
    var httpcontext = {
        'request': {
            'http_proxy': 'http://127.0.0.1:8888',
            'headers': {'Content-Type': 'image/jpeg;charset=UTF-8', 'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8'}
        }, 'response': {'charset': 'buffer'}
    };
    httpClient.get('http://202.109.191.178:8081/wt-web/captcha?0.4344325280246917', function (err, body, res, httpContext) {
        fs.writeFileSync("/Users/jgm/Downloads/chrome/captcha.png", body);
    }, httpcontext);
1.0.37

11 months ago

1.0.38

10 months ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.31

4 years ago

1.0.29

4 years ago

1.0.30

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago