我有以下Python代码
import requests
url = 'https://moe-register.emis.gov.eg/account/authenticate'
data = {'EmailAddress': '[email protected]'}
response = requests.post(url, data=data)
print('Final URL After Redirection:', response.url)
我需要此代码的等效 vba 来打印响应 url。在 python 中,它打印类似的东西
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=e6ffeec9-aee3-480d-9d60-ddbdce181893&response_type=id_token&redirect_uri=https%3a%2f%2fmoe-register.emis.gov.eg%2faccount%2ftrust&scope=openid%20email%20profile&response_mode=form_post&state=315db325-bdb7-4e8a-a7ac-0559970943a7&nonce=018c05a4-73f5-459b-8626-5ae5d1b3b0f6&[email protected]
但在 vba 中我没有得到这个
这是VBA代码
Sub Test()
Dim http As Object
Dim url As String
Dim email As String
Dim status As Long
Dim locationHeader As String
Dim responseBody As String
Dim maxRedirects As Integer
Dim redirectCount As Integer
url = "https://moe-register.emis.gov.eg/account/authenticate"
email = "[email protected]"
Set http = CreateObject("WinHTTP.WinHTTPRequest.5.1")
maxRedirects = 10
redirectCount = 0
Do
http.Open "POST", url, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.send "EmailAddress=" & email
status = http.status
If status >= 300 And status < 400 Then
locationHeader = http.getResponseHeader("Location")
url = locationHeader
redirectCount = redirectCount + 1
Else
responseBody = http.responseText
Exit Do
End If
Loop While redirectCount < maxRedirects
MsgBox "Final URL After Redirection: " & url
If Len(responseBody) > 1024 Then
MsgBox "Response Body (First 1024 characters): " & Left(responseBody, 1024)
Else
MsgBox "Response Body: " & responseBody
End If
Set http = Nothing
End Sub
我找到了解决问题的方法
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Option(WinHttpRequestOption_EnableRedirects) = False
.Option(WinHttpRequestOption_UserAgentString) = "curl"
.Open "POST", "https://moe-register.emis.gov.eg/account/authenticate", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send "EmailAddress=" & sEmail
sLocation = .getResponseHeader("Location")
End With