当前位置:   article > 正文

Typescript中的Http请求_typescript http

typescript http
  1. import * as http from 'http';
  2. export class HttpRequest{
  3. url: string;
  4. private path: string;
  5. private host: string;
  6. private args: Array<Array<string>>;
  7. constructor(url: string, args?: string){
  8. this.url = url;
  9. this.processUrl(this.url);
  10. if(!!args){
  11. this.processArgs(args);
  12. }
  13. this.args = [];
  14. }
  15. private processUrl(url: string): void {
  16. let tld: number = url.lastIndexOf('.')
  17. let sep: number = url.indexOf('/', tld);
  18. this.host = url.slice(0, sep);
  19. this.path = url.slice(sep+1);
  20. }
  21. private processArgs(args: string): void {
  22. let sep: number = args.indexOf('&');
  23. if(sep < 0){
  24. return ;
  25. }
  26. let argpair: string = args.slice(0, sep);
  27. let apsep: number = argpair.indexOf('=');
  28. let k: string = argpair.slice(0, apsep);
  29. let v: string = argpair.slice(apsep+1);
  30. this.args.push([k,v]);
  31. this.processArgs(args.slice(sep+1));
  32. }
  33. private preparePath(): string {
  34. let path: string = `?`;
  35. this.args.forEach((arg: Array<string>, i: number): void => {
  36. let k: string = arg[0];
  37. let v: string = arg[1];
  38. path += k + '=' + v;
  39. if(i == this.args.length-1){
  40. return ;
  41. }
  42. path += '&';
  43. });
  44. return path;
  45. }
  46. public addArg(key: string, value: string): void {
  47. try{
  48. this.args.push([key,value]);
  49. } catch(err) {
  50. console.log(err);
  51. }
  52. }
  53. public addArgs(args: Array<Array<string>>): void {
  54. args.forEach((arg: Array<string>): void => {
  55. this.args.push(arg);
  56. });
  57. }
  58. public get(cb: (res: any) => any): void {
  59. let opts = {
  60. 'host': this.host,
  61. 'path': `/${this.path}/${this.preparePath()}`
  62. };
  63. http.request(opts, (r: http.IncomingMessage): void => {
  64. let data = '';
  65. r.on('data', (chunk: string): void => {
  66. console.log('Got chunk: ' + chunk);
  67. data += chunk;
  68. });
  69. r.on('end', (): void =>{
  70. console.log('Response has ended');
  71. console.log(data);
  72. cb(data);
  73. });
  74. r.on('error', (err): void => {
  75. console.log('Following error occured during request:\n');
  76. console.log(err);
  77. })
  78. }).end();
  79. }
  80. public test(): void {
  81. console.log(this.preparePath());
  82. console.log(`/${this.path}/${this.preparePath()}`);
  83. }
  84. }

调用:

  1. / Test httpRequest
  2. import { HttpRequest } from './httpRequest';
  3. const request = new HttpRequest('www.random.org/integers');
  4. request.addArg('num', '1');
  5. request.addArg('min', '1');
  6. request.addArg('max', '50');
  7. request.addArg('col', '1');
  8. request.addArg('base', '10');
  9. request.addArg('format', 'plain');
  10. request.addArg('rnd', 'new');
  11. request.test();
  12. request.get((res: string): void => {
  13. console.log('Response received: ' + res);
  14. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/152617
推荐阅读
相关标签
  

闽ICP备14008679号