0

I'm trying to take the following curl command and use it in my vb.net page to connect to 3rd party.

curl -X POST -i -H "Accept: application/json" -u someusername:somepassword "https://domain.com/xyz/api/user" -d 'firstName=john&lastName=smith&ssn=111223333&currentAddress=1 Main ST&currentCity=somecity&currentState=WA&currentZip=99999'

Command above work fine in cygwin and I've tried to convert it to following but no luck. I tried encoding the password but that didn't help either. I would appreciate any help with this. Thank you!

Dim url As String = "https://domain.com/xyz/api/user"
Dim data As String = "{""CurrentAddress"":""1 Main St"", ""CurrentCity"":""Somecity"",""CurrentState"":""WA"",""CurrentZip"":""99999"",""FirstName"":""john"",""LastName"":""smith"",""ssn"":""111223333""}"
Dim myReq As WebRequest = WebRequest.Create(url)
myReq.Method = "POST"
myReq.ContentLength = data.Length
myReq.ContentType = "application/json"
myReq.Credentials = New NetworkCredential("someusername", "somepassword")

Using ds As Stream = myReq.GetRequestStream()
    ds.Write(System.Text.ASCIIEncoding.Default.GetBytes(data), 0, data.Length)
End Using


Dim wr As WebResponse = myReq.GetResponse()
Dim receiveStream As Stream = wr.GetResponseStream()
Dim reader As New StreamReader(receiveStream, Encoding.UTF8)
Dim content As String = reader.ReadToEnd()
MsgBox(content)
user3760180
  • 1
  • 1
  • 2
  • Do you get an error with your code, or not what you expect as a response? – InbetweenWeekends Jun 20 '14 at 18:53
  • I think my problem is that its not authenticating properly with the username/password, therefore the server isn't responding with the correct data. I can copy paste the exact response from the server if you think that will help. Is there an easy way view what the server is seeing from this request? – user3760180 Jun 21 '14 at 00:54
  • Is there an easy way? - not unless - and it doesn't sound like - you have access to that piece. It might help, but I don't know for certain. Have you tried anything from here? --- http://stackoverflow.com/questions/5152723/curl-with-user-authentication-in-c-sharp – InbetweenWeekends Jun 21 '14 at 04:33

1 Answers1

0

After messing with it for two days, I was able to get it to work. here is the solution that I came up with.

    Dim paramName As String() = New String(6) {"firstName", "lastName", "ssn", "currentAddress", "currentCity", "currentState", "currentZip"}
    Dim paramVal As String() = New String(6) {"John", "Smith", "111223333", "1 main st", "any city", "dc", "11111"}
    Dim result As String
    result = HttpPost("https://domain.com/XYZ/api/user", paramName, paramVal)



Private Shared Function HttpPost(url As String, paramName As String(), paramVal As String()) As String
    Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
    req.Method = "POST"
    req.ContentType = "application/x-www-form-urlencoded"

    req.Headers("Authorization") = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("myusername:mypassword"))


    ' Build a string with all the params, properly encoded.
    ' We assume that the arrays paramName and paramVal are
    ' of equal length:
    Dim paramz As New StringBuilder()
    For i As Integer = 0 To paramName.Length - 1
        paramz.Append(paramName(i))
        paramz.Append("=")
        paramz.Append(HttpUtility.UrlEncode(paramVal(i)))
        paramz.Append("&")
    Next

    ' Encode the parameters as form data:
    Dim formData As Byte() = UTF8Encoding.UTF8.GetBytes(paramz.ToString())
    req.ContentLength = formData.Length

    ' Send the request:
    Using post As Stream = req.GetRequestStream()
        post.Write(formData, 0, formData.Length)
    End Using

    ' Pick up the response:
    Dim result As String = Nothing
    Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
        Dim reader As New StreamReader(resp.GetResponseStream())
        result = reader.ReadToEnd()
    End Using

    Return result
End Function
user3760180
  • 1
  • 1
  • 2