VB + API 获取 IE 的 "代理服务器" 配制

Option Explicit
Private Type INTERNET_PROXY_INFO
    dwAccessType    As Long
    lpszProxy       As Long
    lpszProxyBypass As Long
End Type
Private Const INTERNET_OPTION_PROXY = 38
Private Declare Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" (ByVal hInternet As Long, ByVal dwOption As Long, ByRef lpBuffer As Any, ByVal dwBufferLength As Long) As Long
Private Declare Function InternetQueryOption Lib "wininet.dll" Alias "InternetQueryOptionA" (ByVal hInternet As Long, ByVal dwOption As Long, ByRef lpBuffer As Any, ByRef dwBufferLength As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef pDest As Any, ByRef pSource As Any, ByVal Length As Long)
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Public Function GetProxyAddressAndPort(Optional ProxyType As String) As String
Dim ProxyInfo As INTERNET_PROXY_INFO
Dim arrBuffer() As Byte
Dim strAddress As String
Dim strBypass As String
ReDim arrBuffer(0 To 4095)
InternetQueryOption 0&, INTERNET_OPTION_PROXY, arrBuffer(0), UBound(arrBuffer) - LBound(arrBuffer) + 1
CopyMemory ProxyInfo, arrBuffer(0), LenB(ProxyInfo)
strAddress = VBA.Space(lstrlen(ProxyInfo.lpszProxy))
lstrcpy ByVal strAddress, ProxyInfo.lpszProxy
'不使用代理的地址 strBypass
strBypass = VBA.Space(lstrlen(ProxyInfo.lpszProxyBypass))
lstrcpy ByVal strBypass, ProxyInfo.lpszProxyBypass
'GetProxyAddressAndPort = VBA.Trim(VBA.Replace(strAddress, " ", ";")) ' & ";" & VBA.Replace(strBypass, " ", ";"))
GetProxyAddressAndPort = VBA.Trim(VBA.Replace(strAddress, " ", ";") & ";" & VBA.Replace(strBypass, " ", ";"))
Dim ProxyArray() As String
ProxyArray = Split(GetProxyAddressAndPort, ";")
Dim i As Integer
For i = LBound(ProxyArray) To UBound(ProxyArray)
    If ProxyArray(i) Like "*" & ProxyType & "=*" Then
       GetProxyAddressAndPort = VBA.Replace(ProxyArray(i), ProxyType & "=", "")
       Exit For
    End If
Next i
End Function

Private Sub Command1_Click()
MsgBox GetProxyAddressAndPort("http")
MsgBox GetProxyAddressAndPort("ftp")
MsgBox GetProxyAddressAndPort("gopher")
End Sub

'编程配置 IE 的 "代理服务器" 要用到 InternetSetOption
'一个例子下载:
'http://www.vbip.com/forum/files/webbrowser_proxy.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值