1

Need help in solving error in HTTP GET request [closed]

I have the following code in VB.NET framework which should pull the data in json format and add it to a list view control using GET request. However I am getting following error:

System.Threading.Tasks.Task.FromResult(System.Net.Http.HttpResponseMessage)

Dim client = New HttpClient()
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{Subscription-Key}")
Dim uri = "https://example.com/" & SearchKey
Dim response = client.GetAsync(uri)
lstMer.Items.Add(response.ToString)

Initially I wrote the following code in VBA and it was working fine:

Dim jsonText as String
Set req = New MSXML2.ServerXMLHTTP60
key_id = "{Subscription-Key}"
key_header_name = "Subscription-Key-Id"
liveURL = "https://example.com/" & SearchKey
req.Open "GET", liveURL, False
req.setRequestHeader {Subscription-Key-Id}, {Subscription-Key}
req.setRequestHeader "Host", "site Address"
req.send
jsonText = req.responseText

The requirement is to get the headers from URL and fill a list view.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    A little more details in the error please, `System.Threading.Tasks.Task.FromResult(System.Net.Http.HttpResponseMessage)` is a Type, not an error. Which line throws the exception? – djv Sep 15 '21 at 19:35
  • 2
    Perhaps you need to mark your method as `Async` and change the line to `Dim response = Await client.GetAsync(uri)` – djv Sep 15 '21 at 19:38
  • i am getting following error: 'Await' can only be used within an Async method. Consider marking this method with the 'Async' modifier and changing its return type to 'Task'. – Joseph Kachappilly Sep 15 '21 at 19:57
  • Read again the beginning of my last comment. See my answer (`Private Async Sub Foo()`) – djv Sep 15 '21 at 19:58

1 Answers1

1

I think I understand. You don't get an error

System.Threading.Tasks.Task.FromResult(System.Net.Http.HttpResponseMessage)

rather you get that as a result of response.ToString(). This is because you are calling client.GetAsync(uri) directly, which returns a Task.FromResult<TResult>. However, it is meant to be run async, and there are some convenient keywords for achieving this, namely Async / Await. If you await the task, instead of returning a Task.FromResult<TResult>, it will return a TResult. So change your code to do just that

Private Async Sub Foo()
    Dim client = New HttpClient()
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{Subscription-Key}")
    Dim uri = "https://example.com/" & SearchKey
    Dim response = Await client.GetAsync(uri)
    lstMer.Items.Add(response.ToString)
End Sub

See HttpClient.GetAsync and about what it returns Task.FromResult

djv
  • 15,168
  • 7
  • 48
  • 72
  • I am getting following errors while running the code: 1. WebException: The underlying connection was closed: An unexpected error occurred on a send. 2. IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 3. SocketException: An existing connection was forcibly closed by the remote host – Joseph Kachappilly Sep 15 '21 at 20:13
  • @JosephKachappilly could be anything? See https://stackoverflow.com/a/34678607/832052 – djv Sep 15 '21 at 20:20
  • @JosephKachappilly The good news is you're making progress. By assigning response to `client.GetAsync(uri)` previously, the request was never actually being made. The fact that you have errors now means it is! But it signals a problem fundamentally different from the one in your question. – djv Sep 15 '21 at 20:25
  • I want a simple solution, the same source is working in VBA. I need to use a GET request to receive the json response and then parse the response to table format. I converted C# ocde to VB.NET. Following is the Curl code sample given in the documentation `curl -v -X GET "https://cds.sbp.site.com/Engagements/{engagementId}" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{body}"` – Joseph Kachappilly Sep 15 '21 at 20:36