@@ -4,6 +4,7 @@ package ehttp
4
4
import (
5
5
"bytes"
6
6
"crypto/tls"
7
+ "encoding/base64"
7
8
"encoding/json"
8
9
"errors"
9
10
"fmt"
@@ -37,6 +38,8 @@ type Ehttp struct {
37
38
E代理方式 int
38
39
//代理ip
39
40
Proxy string
41
+ //代理用户名:代理密码
42
+ ProxyAuth string
40
43
//全局头信息
41
44
全局头信息 string
42
45
//默认头信息
@@ -431,6 +434,7 @@ func (this *Ehttp) setObj() *Ehttp {
431
434
//等待接收服务端的回复的头域的最大时间。零值表示不设置超时。
432
435
ResponseHeaderTimeout : time .Duration (this .TimeOut ) * time .Second ,
433
436
Proxy : nil ,
437
+ ProxyConnectHeader : nil ,
434
438
//如果DisableKeepAlives为真,会禁止不同HTTP请求之间TCP连接的重用。
435
439
DisableKeepAlives : false ,
436
440
DisableCompression : false ,
@@ -443,8 +447,18 @@ func (this *Ehttp) setObj() *Ehttp {
443
447
trans .Proxy = func (_ * http.Request ) (* url.URL , error ) {
444
448
return url .Parse (this .Proxy )
445
449
}
450
+ if len (this .ProxyAuth ) > 3 && strings .Index (this .ProxyAuth , ":" ) > - 1 {
451
+ encodedAuth := base64 .StdEncoding .EncodeToString ([]byte (this .ProxyAuth ))
452
+
453
+ trans .ProxyConnectHeader = http.Header {
454
+ "Proxy-Authorization" : []string {"Basic " + encodedAuth },
455
+ }
456
+ }else {
457
+ trans .ProxyConnectHeader = nil
458
+ }
446
459
}
447
460
461
+
448
462
client := & http.Client {
449
463
Transport : trans ,
450
464
CheckRedirect : func (req * http.Request , via []* http.Request ) error {
@@ -459,20 +473,22 @@ func (this *Ehttp) setObj() *Ehttp {
459
473
return this
460
474
}
461
475
462
- // SetProxy 设置代理访问
476
+ // SetProxy 设置代理访问, 如果没有 用户名 密码,则留"",带用户名和密码的用法:SetProxy("127.0.0.1:8888","user:pass") 不带的用法:SetProxy("127.0.0.1:8888","")
463
477
//
464
478
// SetProxy("http://127.0.0.1:8888")
465
- func (this * Ehttp ) SetProxy (proxy string ) * Ehttp {
479
+ func (this * Ehttp ) SetProxy (proxy string , proxyAuth string ) * Ehttp {
466
480
//检查 前面是否带有 http:// 或者 https:// 如果没有则自动添加 socks5:// 则不自动添加
467
481
if strings .Index (proxy , "://" ) == - 1 {
468
482
proxy = "http://" + proxy
469
483
}
470
484
471
485
this .Proxy = proxy
486
+ this .ProxyAuth = proxyAuth
487
+
472
488
return this
473
489
}
474
- func (this * Ehttp ) E设置全局HTTP代理 (proxy string ) * Ehttp {
475
- return this .SetProxy (proxy )
490
+ func (this * Ehttp ) E设置全局HTTP代理 (proxy string , proxyAuth string ) * Ehttp {
491
+ return this .SetProxy (proxy , proxyAuth )
476
492
}
477
493
func (this * Ehttp ) SetTimeOut (超时时间 int ) * Ehttp {
478
494
this .TimeOut = 超时时间
0 commit comments