none
Convert curl(php) code to vb.net RRS feed

  • Question

  • Hello there i want to convert this php code to vb.net basically this is a url  in vb.net i use  &  for urls  authorization but i don't know what this line is  is there anyone who can help me on this  ?? 

    here is the code :

    $ curl -X POST https://rest.messagebird.com/messages \
    -H 'Authorization: AccessKey test_gshuPaZoeEG6ovbc8M79w0QyM' \
    -d "recipients=31612345678" \
    -d "originator=YourName" \
    -d "body=This is a test message"

    kind regards 


    vinay pant

    Thursday, June 30, 2016 7:13 AM

Answers

  • Hello there i want to convert this php code to vb.net basically this is a url  in vb.net i use  &  for urls  authorization but i don't know what this line is  is there anyone who can help me on this  ?? 

    here is the code :

    $ curl -X POST https://rest.messagebird.com/messages \
    -H 'Authorization: AccessKey test_gshuPaZoeEG6ovbc8M79w0QyM' \
    -d "recipients=31612345678" \
    -d "originator=YourName" \
    -d "body=This is a test message"

    kind regards 


    vinay pant


    In .Net you would use an instance of HttpClient and the PostAsync method (or a HttpWebRequest/Response pair in older versions of the framework).  You'll need to configure an instance of HttpStringContent to pass to the PostAsync method.  This content object will contain the field values to submit to the form.  The -H line of the PHP will need to be added as a custom header on the content instance.  The rest you can concatenate into a string as your are used to doing, e.g.  recipients=31612345678&originator=YourName&body=This is a test message

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, June 30, 2016 8:10 PM
    Moderator
  • The above is very close.  The only mistake is that you do not need to declare a variable for the HttpHeaders.  The error is because you did not use the New keyword to create a new instance of the HttpHeaders object, but that isn't what you want to do anyway.

    You want to use the instance of the HttpHeaders object which already exists on the HttpContent instance.  In the code above, delete the line "Dim head As HttpHeaders" and then replace the head.Add() with "content.Headers.Add()" so that you are adding to the headers collection already associated with the content object.

    That should correct your code.  Then the only thing may be adjusting the strings themselves until they are formatted correctly.  For instance, I don't know if you need the colon at the end of the header name, and I'm not sure if your content body string needs quotes around the post variable values.  But those things should be easy enough to resolve once the code complies and executes.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by repulsive1 Tuesday, July 5, 2016 2:23 AM
    Monday, July 4, 2016 6:50 PM
    Moderator

All replies

  • Hello there i want to convert this php code to vb.net basically this is a url  in vb.net i use  &  for urls  authorization but i don't know what this line is

    Some examples here:
    https://msdn.microsoft.com/en-us/library/aa479002.aspx

    You will probably get more responses in the ASP forum:  ASP.Net forums (ASP and VB for MVC)

    Thursday, June 30, 2016 7:57 AM
  • Also note that not everyone knows what CURL is. If you explain what it is doing for you then someone that knows how to do that in .Net can help even if they don't know what CURL is.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, June 30, 2016 5:39 PM
  • Hello there i want to convert this php code to vb.net basically this is a url  in vb.net i use  &  for urls  authorization but i don't know what this line is  is there anyone who can help me on this  ?? 

    here is the code :

    $ curl -X POST https://rest.messagebird.com/messages \
    -H 'Authorization: AccessKey test_gshuPaZoeEG6ovbc8M79w0QyM' \
    -d "recipients=31612345678" \
    -d "originator=YourName" \
    -d "body=This is a test message"

    kind regards 


    vinay pant


    In .Net you would use an instance of HttpClient and the PostAsync method (or a HttpWebRequest/Response pair in older versions of the framework).  You'll need to configure an instance of HttpStringContent to pass to the PostAsync method.  This content object will contain the field values to submit to the form.  The -H line of the PHP will need to be added as a custom header on the content instance.  The rest you can concatenate into a string as your are used to doing, e.g.  recipients=31612345678&originator=YourName&body=This is a test message

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, June 30, 2016 8:10 PM
    Moderator
  • Thank you so much for the help i also thought that this would be the correct form 

    embedding these in the main url  as &  

    but the main problem is with header  


    vinay pant

    Friday, July 1, 2016 5:09 AM
  • Thank you so much for the help i also thought that this would be the correct form 

    embedding these in the main url  as &  

    but the main problem is with header  


    vinay pant

    Don't embed anything in the URL.  The URL should just be "https://rest.messagebird.com/messages".  When you create the new instance of HttpContent, you supply the concatenated string as the constructor parameter.  Something like:

    Dim content = New HttpContent("recipients=31612345678&originator=YourName&body=This is a test message")

    For the header you'll want to access the HttpContent.Headers property and add the "Authorization" header and it's value with the Add() method.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, July 1, 2016 10:17 PM
    Moderator
  • Hello there sir thank you so much for your guidance 

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Dim instance As New HttpClient
                Dim requestUri As String = "https://rest.messagebird.com/messages"
    
                Dim content As HttpContent = New StringContent("recipients =009198499650849971" & "originator=YourName" & "body=This is a test message")
                Dim head As HttpHeaders
                head.Add("Authorization:", "AccessKey live")
    
                Dim returnValue As Task(Of HttpResponseMessage)
    
                returnValue = instance.PostAsync(requestUri,
                    content)
                MsgBox(returnValue)
            Catch ex As Exception
                MsgBox("i am error  " & ex.Message)
            End Try
        End Sub

    this is the code i have done so far but i am facing some issues  if you could help on this i would be very grateful to you  

    when i run this code i face error  as ::object reference not set to an instance of an object  

    when i remove the headers then i face this error :: argument prompt cannot be converted to type string  

    i know that headers should have a default value  while defining it  but as i am new to this topic i don't  know how to do it  i uses == to assign a value but this does not helps of even if i declare it as new  it throws a error 

    kind regards  


    vinay pant

    Sunday, July 3, 2016 2:18 PM
  • The above is very close.  The only mistake is that you do not need to declare a variable for the HttpHeaders.  The error is because you did not use the New keyword to create a new instance of the HttpHeaders object, but that isn't what you want to do anyway.

    You want to use the instance of the HttpHeaders object which already exists on the HttpContent instance.  In the code above, delete the line "Dim head As HttpHeaders" and then replace the head.Add() with "content.Headers.Add()" so that you are adding to the headers collection already associated with the content object.

    That should correct your code.  Then the only thing may be adjusting the strings themselves until they are formatted correctly.  For instance, I don't know if you need the colon at the end of the header name, and I'm not sure if your content body string needs quotes around the post variable values.  But those things should be easy enough to resolve once the code complies and executes.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by repulsive1 Tuesday, July 5, 2016 2:23 AM
    Monday, July 4, 2016 6:50 PM
    Moderator
  • Thank you so much for this sir now it is working fine i just have to edit the headers string  as colon is not it's part  :) 

    i really appreciate your help  

    kind regards 


    vinay pant

    Tuesday, July 5, 2016 2:23 AM
  • Dear Moderator,

    Please Help Me..

    I have problem similiar to above..

    I have cURL like bellow:

    curl -X POST  https://rest.nexmo.com/sms/json \
    -d api_key=629f9e21 \
    -d api_secret=GEAnTJu4GHPG9J5b \
    -d to=6281339454565 \
    -d from="NEXMO" \
    -d text="Hello from Nexmo"

    and convert script above to VB.Net like this bellow, I don't know where should I put api_secret=GEAnTJu4GHPG9J5b 

    Dim instance As HttpClient
            Dim requestUri As String = "https://rest.nexmo.com/sms/json"
    
            Dim content As HttpContent = New StringContent("to =6281339454565" & "from=VRNGQ" & "text=This is a test message")
            content.Headers.Add("api_key", "629f7e21")
            Dim returnValue As Task(Of HttpResponseMessage)
    
            returnValue = instance.PostAsync(requestUri,
                content)
            MsgBox(returnValue)

    Sunday, September 15, 2019 3:37 PM