将cURL转换为ASP.Net

时间:2018-07-24 14:17:55

标签: asp.net curl

我正在寻找一种将以下cURL转换为ASP.Net的方法。

curl -F f=@example.pdf "https://pdftables.com/api?key=ZZZ999&format=xml"

我已经广泛使用以下功能来获取几乎所有的URL /内容,但是我对如何包含位于托管Web服务器上的文件一无所知。

Public Shared Function GetRemoteURL(getURL As String) As String
    Dim objReq As HttpWebRequest
    Dim objRsp As HttpWebResponse = Nothing
    Dim objRdr As StreamReader
    Dim objRes As String = ""
    Try
        objReq = DirectCast(WebRequest.Create(getURL), HttpWebRequest)
        objRsp = DirectCast(objReq.GetResponse(), HttpWebResponse)
        objRdr = New StreamReader(objRsp.GetResponseStream())
        objRes = objRdr.ReadToEnd()
    Catch
        objRes = ""
    Finally
        If Not objRsp Is Nothing Then objRsp.Close()
    End Try
    Return objRes.ToString()
End Function

任何建议/指导将不胜感激。

约翰

1 个答案:

答案 0 :(得分:2)

您必须设置请求正文,例如:

Public Function GetRemoteURL(getURL As String) As String
    Dim objReq As HttpWebRequest
    Dim objRsp As HttpWebResponse = Nothing
    Dim objRdr As StreamReader
    Dim objRes As String = "" 
    Try
        objReq = DirectCast(WebRequest.Create(getURL), HttpWebRequest)

        objReq.Method = "POST"
        Dim postData = "f=@example.pdf"
        Dim encoding As New ASCIIEncoding()
        Dim bytes = encoding.GetBytes(postData) 
        objReq.ContentLength = bytes.Length
        Dim stream = objReq.GetRequestStream()
        stream.Write(bytes, 0, bytes.Length)

        objRsp = DirectCast(objReq.GetResponse(), HttpWebResponse)
        objRdr = New StreamReader(objRsp.GetResponseStream())
        objRes = objRdr.ReadToEnd()
    Catch
        objRes = ""
    Finally
        If Not objRsp Is Nothing Then objRsp.Close()
    End Try
    Return objRes.ToString()
End Function

注意:我没有对此进行测试,因此可能无法直接工作。