Llamo al siguiente método de controlador webapi2 desde un método de controlador mvc:
<HttpPost>
Public Function TestApiCall(<FromBody> screenerRequest As JsonBaseContainer) As IHttpActionResult
Dim response = Me.Request.CreateResponse(HttpStatusCode.OK)
response.Content = New StringContent("{""foo"":""bar""}", Encoding.UTF8, "text/plain")
Return ResponseMessage(response)
End Function
Lo llamo desde esta rutina en el servidor asp.net:
Public Async Function PostJsonContent(baseUri As String, requestUri As String, content As String, Optional timeout As Integer = 15, Optional failedResponse As String = "", Optional ignoreSslCertErrors As Boolean = False) As Task(Of String)
Return Await PostJsonContent(baseUri, requestUri, New StringContent(content, Encoding.UTF8, "application/json"), timeout, failedResponse, ignoreSslCertErrors)
End Function
Public Async Function PostJsonContent(baseUri As String, requestUri As String, content As HttpContent, Optional timeout As Integer = 15, Optional failedResponse As String = "", Optional ignoreSslCertErrors As Boolean = False) As Task(Of String)
Dim httpResponse As HttpResponseMessage
Using handler = New WebRequestHandler
If ignoreSslCertErrors Then
handler.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(Function(sender, cert, chain, policyErrors) True)
End If
Using client = New HttpClient(handler)
If Not String.IsNullOrWhiteSpace(baseUri) Then
client.BaseAddress = New Uri(baseUri)
End If
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.Timeout = New TimeSpan(TimeSpan.FromSeconds(timeout).Ticks)
httpResponse = Await client.PostAsync(requestUri, content)
If httpResponse.IsSuccessStatusCode Then
Dim response = Await httpResponse.Content.ReadAsStringAsync
If Not String.IsNullOrWhiteSpace(response) Then
Return response
End If
End If
End Using
End Using
Return failedResponse
End Function