如何将该.Net Post函数转换为VBA?

时间:2019-04-21 00:11:21

标签: .net vba vb.net

我正在尝试将API用于称为AceProject的工具,以便通过VBA创建项目:http://api.aceproject.com/explorer/?fct=createorcopyproject

我的代码使用GetHTTP可以正常工作,但是为了允许更长的项目描述,我想改用Post方法(这样我就不受URL字符限制)。但是我不熟悉如何将他们的代码示例转换为VBA,并且在我的网站上没有我所链接的文档。

这是我的函数调用:

    Dim RawJSON As String: RawJSON = PostHTTP("?fct=createorcopyproject&guid=" & GUID_ & _
    "&projectnumber=" & ProjectNum & "&projectname=" & ProjectName & "&projecttype=" & ProjectType & "&projectdesc=" & ProjectDesc & _
    "&nexttasknumber=" & TaskNum & _
    "&budgethours=0&budgetcost=0&estimatedstartdate=" & StartDate & _
    "&estimatedhours=0&estimatedexpenses=0&projecttemplate=0&defaultestimatedtime=0&defaulttaskstartdate=2&defaulttaskenddate=2&defaulttaskactualdates=2&projecttemplateid=" & TemplateID & _
    "&keeptemplatelink=False&copyprojectassignments=True&copyprojectdocuments=True&copyforumtopics=True&copytasks=True&adjusttaskdates=True&copytaskdocuments=True&copytaskassignments=True&marklevel=1&format=JSON")

还有我的非工作帖子功能:

Private Function PostHTTP(ByVal PostStr As String) As String
    On Error Resume Next
    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", "http://api.aceproject.com/", False
        .SetRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .Send (PostStr)
        PostHTTP = .ResponseText
    End With
End Function

这是他们.NET示例中的相关代码:

       strXML = CallHttp("http://api.aceproject.com/", sb.ToString)

    Private Shared Function CallHttp( ByVal url As String, _ 
                                      ByVal params As String) As String
        Dim loHttp As System.Net.HttpWebRequest
        loHttp = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
        loHttp.Method = "POST"
        Dim requestWriter As System.IO.StreamWriter = New System.IO.StreamWriter(loHttp.GetRequestStream())

        If Not String.IsNullOrEmpty(params) Then
            requestWriter.Write(params)
        End If
        requestWriter.Close()
        loHttp.ContentType = "application/x-www-form-urlencoded"
        loHttp.Headers.Set("Pragma", "no-cache")
        loHttp.AllowAutoRedirect = True
        loHttp.KeepAlive = True
        loHttp.Timeout = 30 * 1000

        Dim loWebResponse As System.Net.HttpWebResponse = CType(loHttp.GetResponse(), System.Net.HttpWebResponse)
        Dim enc As Encoding = System.Text.Encoding.UTF8
        Dim loResponseStream As System.IO.StreamReader = New System.IO.StreamReader(loWebResponse.GetResponseStream(), enc)
        Dim lcHtml As String = loResponseStream.ReadToEnd()

        loWebResponse.Close()
        loResponseStream.Close()

        Return lcHtml
    End Function

是否可以使用上述信息通过邮寄而不是get来发送URL字符串?

1 个答案:

答案 0 :(得分:0)

这是最终的工作版本(创建了一个具有20k字符描述的测试项目),希望这可以帮助某人。

函数调用:

def eq(a, b):
    try:
        return np.all(a == b)
    except ValueError:
        pass

    try:
        if not len(a) == len(b):
            return False

        if type(a) == np.ndarray:
            return np.array_equal(a, b)
        if isinstance(a, dict):
            return all(eq(v, b[k]) for k, v in a.items())
        else:
            return all(eq(aa, bb) for aa, bb in zip(a, b))
    except (TypeError, KeyError):
        return False

发布功能:

Private Const BASE_URL = "http://api.aceproject.com/"

    Dim RawJSON As String: RawJSON = PostHTTP(BASE_URL, "fct=createorcopyproject&guid=" & GUID_ & _
    "&projectnumber=" & ProjectNum & "&projectname=" & ProjectName & "&projecttype=" & ProjectType & "&projectdesc=" & ProjectDesc & _
    "&nexttasknumber=" & TaskNum & _
    "&budgethours=0&budgetcost=0&estimatedstartdate=" & StartDate & _
    "&estimatedhours=0&estimatedexpenses=0&projecttemplate=0&defaultestimatedtime=0&defaulttaskstartdate=2&defaulttaskenddate=2&defaulttaskactualdates=2&projecttemplateid=" & TemplateID & _
    "&keeptemplatelink=False&copyprojectassignments=True&copyprojectdocuments=True&copyforumtopics=True&copytasks=True&adjusttaskdates=True&copytaskdocuments=True&copytaskassignments=True&marklevel=1&format=JSON")