0

MY Call code is in VB not C#:

I need to call an api service that is in C# from VB.net code and send 3 variables. When I use this code it is giving back error as I think where I send variables is not correct. Can you please guild me a correct way to send multiple variable from VB to call a service?

This is VB method to call the service:

        Dim apiUrl As String
        apiUrl = ConfigurationManager.AppSettings("URL").ToString()
        Dim hwr As HttpWebRequest
        hwr = WebRequest.Create(apiUrl + "api/Connect/acc")
        Dim postData = "Id =" & objSession.CurrentObject.Object_ID.ToString() & "&UserName =" & name & "&Reason =" & statusReason
        hwr.Method = "POST"

        Try
            Using wr As WebResponse = hwr.GetResponse()
                If CType(wr, HttpWebResponse).StatusCode = HttpStatusCode.OK Then
                    Using dataStream As Stream = wr.GetResponseStream()
                        Using reader As New StreamReader(dataStream)
                            Dim responseFromServer As String = reader.ReadToEnd()
                            reader.Close()
                        End Using
                    End Using
                    wr.Close()
                End If
            End Using
        Catch ex As Exception
            '...handle error...
        End Try

and this is the method that is am calling in the API:

    [RoutePrefix("api/Connect")]
    Class
    .......
    [HttpPost]
    [Route("acc")]
    public void CancelTransaction(int Id, string UserName, string Reason)
    {
    ...
    }
nik
  • 514
  • 2
  • 6
  • 19
  • what error are you getting, exactly? – ADyson May 15 '19 at 16:31
  • 1
    btw you have a typo in the postData" variable definition: `"Id ="` should be `"Id="`, you can't have spaces in the URL like that. Same for Username and Reason. – ADyson May 15 '19 at 16:32
  • Furthermore, you never actually appear to attach the data to the request... you define the "postData" variable, but you never do anything with it. – ADyson May 15 '19 at 16:33
  • 1
    But anyway, overall what you're doing is a bit legacy. There are nicer ways to make a HTTP request You might find this question useful: https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request (obviously you can easily convert the sample code there from C# to VB with an online tool, if needed) – ADyson May 15 '19 at 16:34
  • 2
    Possible duplicate of [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – ADyson May 15 '19 at 16:36
  • @ADyson Thank you for reply but most of the code I find are in C#, my code that is calling method is in VB.net – nik May 15 '19 at 16:41
  • Look again at my earlier comment: "you can easily convert the sample code there from C# to VB with an [online tool](https://www.google.com/search?q=c%23+to+vb+converter&rlz=1C1GCEU_enGB821GB821&oq=C%23+to+vb+converter&aqs=chrome.0.0j69i57j69i58j69i65.2375j0j7&sourceid=chrome&ie=UTF-8), if needed". Equally, it probably wouldn't be too hard to re-write it yourself - the code required is quite short and straightforward. And of course the beauty of the .NET runtime is that the same libraries and method calls are available across all the .NET-compatible languages. – ADyson May 15 '19 at 16:42
  • Your user profile says you have C# experience so I assume there's no difficulty for you in understanding the C# code samples. – ADyson May 15 '19 at 16:44

0 Answers0