如何 运行 来自经典 ASP 的 Jenkins 作业?

How to run a Jenkins job from classic ASP?

我搜索了所有地方,但找不到 VBscript 示例,而且我的所有尝试都失败了。我需要 运行 来自经典 ASP 网页 (VBScript) 的 Jenkins 作业。我可以使用下面的代码提交作业,但它 returns 出现 403 碎屑错误。我需要做的是为这项工作提供 user/password (我有),但我不知道如何在 VBScript 中为 Jenkins 设置身份验证。我知道面包屑错误是由于 CSRF 引起的(我读了那么多,但无法将其关闭)并希望身份验证能够解决这个问题。任何帮助深表感谢。提前致谢。

Dim strJenkinsURL, HttpReq
strJenkinsURL = "http://<jenkinsmaster>/job/<myjob>/buildWithParameters?token=test&Description="& strDesc &"&TestEnv="& testEnv

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "POST", strJenkinsURL, False
HttpReq.Send
Response.Write "<br>Status: "& HttpReq.Status & vbNewline
Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

编辑: 根据评论,我尝试添加 Jenkins 登录信息,但我仍然从 Jenkins 收到 403 crumb 错误。我曾尝试寻找解决问题的方法,但没有找到任何 VBScript 示例。这是我现在正在尝试的代码和响应,但我不知道 setRequestHeaders 是否适用于 Jenkins 并且 Jenkins 文档没有任何帮助:

Dim strJenkinsURL
strJenkinsURL = "http://<jenkins master>/job/testjob/buildWithParameters?token=test&Description="& strDesc &"&TestEnv="& testEnv

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "POST", strJenkinsURL, False
HttpReq.setRequestHeader "UserName", "jenkinsuser"
HttpReq.setRequestHeader "Password", "userpassword"
HttpReq.setRequestHeader "Jenkins-Crumb", "<crumbvalue>"
HttpReq.Send
Response.Write "<br>Status: "& HttpReq.Status & vbNewline
Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

状态:403 响应:HTTP 错误 403 访问 /job/testjob/buildWithParameters 时出现问题。原因:请求中未包含有效的面包屑

对于 HTTP POST 请求,URL 指定资源,即 strJenkinsURL = "http://<jenkinsmaster>/job/<myjob>/buildWithParameters

参数作为 .Send 命令的一部分添加:

Dim sParams
sParams = "token=test&Description=" & strDesc & "&TestEnv=" & testEnv
HttpReq.Send(sParams)

正如@Lankymart 指出的,您还需要设置适当的 headers,例如

HttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
HttpReq.setRequestHeader("Content-Length", CStr(Len(sParams)))

在通过一些尝试和错误拼凑出许多建议后,我终于能够使用下面的 VBScript 代码(将变量设置为适当的值,例如 strCrumb)将 Jenkins 作业远程 运行。返回的 Status 将为 201,表示成功。 Base64 函数取自 Base64 Encode String in VBScript

Dim strJenkinsURL, strParam1, strParam2, strCrumb, strUsername, strPassword
strJenkinsURL = "https://<jenkinsURL>/job/testjob/buildWithParameters?token=test&param1="& strParam1 &"&param2="& strParam2

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "POST", strJenkinsURL, False
HttpReq.setOption 2, 13056
HttpReq.setRequestHeader "Jenkins-Crumb", strCrumb
HttpReq.setRequestHeader "Authorization", "Basic "& Base64Encode(strUsername &":"& strPassword)
HttpReq.Send
Response.Write "<br>Status: "& HttpReq.Status & vbNewline
Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

Function Base64Encode(sText)
    Dim oXML, oNode
    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue = Stream_StringToBinary(sText)
    Base64Encode = oNode.text
    Set oNode = Nothing
    Set oXML = Nothing
End Function

Private Function Stream_StringToBinary(Text)
    Const adTypeText = 2
    Const adTypeBinary = 1
    Dim BinaryStream 'As New Stream
    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Type = adTypeText
    BinaryStream.CharSet = "us-ascii"
    BinaryStream.Open
    BinaryStream.WriteText Text
    BinaryStream.Position = 0
    BinaryStream.Type = adTypeBinary
    BinaryStream.Position = 0
    Stream_StringToBinary = BinaryStream.Read
    Set BinaryStream = Nothing
End Function