John Cappelletti
John Cappelletti

Reputation: 81970

Convert cURL to ASP.Net

I'm looking for a way to convert the following cURL to ASP.Net.

curl -F [email protected] "https://pdftables.com/api?key=ZZZ999&format=xml"

I've used the following function extensively to get virtually any URL/content, but I'm lost on how to include a file located on the hosted web server.

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

Any advice/direction would be deeply appreciated.

John

Upvotes: 2

Views: 292

Answers (1)

John Boker
John Boker

Reputation: 83709

You'll have to set the request body, something like:

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 = "[email protected]"
        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

note: i didn't test this so it might not directly work.

Upvotes: 2

Related Questions