1

I have researched a lot and many people mentioned it is not possible to Get request with a body. I managed to get a response from it using Postman. Now I want to write in my code but I have no idea how to do so. I need to get the response but for this url to work, I will need to include the body. Does anyone have any idea how to include the body using C# code?

This is my current code but there is an error -> System.PlatformNotSupportedException: 'WinHttpHandler is only supported on .NET Framework and .NET Core runtimes on Windows. It is not supported for Windows Store Applications (UWP) or Unix platforms.'

 var handler = new WinHttpHandler();
        var client = new HttpClient(handler);

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri("my url"),
            Content = new StringContent("my json body content", Encoding.UTF8, "application/json"),
        };

        var response = await client.SendAsync(request).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        var responsebody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
        string text = responsebody.ToString();
        string[] str = text.Split(new[] { ',', ':' }, StringSplitOptions.RemoveEmptyEntries);
        string result = str[10];
        labelTxt.Text = result;
Charis
  • 81
  • 2
  • 8
  • 1
    Just change to code to `var client = new HttpClient();` - if you're not customising `WinHttpHandler`, then if you use the default constructor for HttpClient it will chose the appropriate inner handler for your platform. – Martin Costello Apr 08 '20 at 10:01
  • use HttpClient https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8 – Pankaj Rawat Apr 08 '20 at 10:10
  • You should probably be using `HttpClientHandler` instead of `WinHttpHandler` which is Windows specific. Although you don't seem to be doing anything with the handler, so just use `new HttpClient()` as @MartinCostello suggests – phuzi Apr 08 '20 at 10:15
  • It's generally a bad idea. It "works", but is against HTTP spec to have any meaning given to the body of a GET request. https://stackoverflow.com/questions/978061/http-get-with-request-body – CoolBots Apr 08 '20 at 15:35
  • 1
    @MartinCostello Hi! It worked after changing my code to what you have mentioned. Thanks :) – Charis Apr 09 '20 at 01:21

1 Answers1

1

You're using the Windows-specific WinHttpHandler type in your code, which you don't need to do as you're not customising it, which is what's causing the exception.

If you change your code to the below, it should work for any platform on .NET Core:

var client = new HttpClient(); // Changed to use the default HttpClient constructor

var request = new HttpRequestMessage
{
    Method = HttpMethod.Get,
    RequestUri = new Uri("my url"),
    Content = new StringContent("my json body content", Encoding.UTF8, "application/json"),
};

var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var responsebody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
string text = responsebody.ToString();
string[] str = text.Split(new[] { ',', ':' }, StringSplitOptions.RemoveEmptyEntries);
string result = str[10];
labelTxt.Text = result;
Martin Costello
  • 7,092
  • 4
  • 48
  • 62
  • Hi, this code did work in retrieving the data but I have another error --> `Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'` . Do you have any idea how to resolve this issue? – Charis Apr 09 '20 at 07:25
  • Sorry, afraid I can't help with that one. I'm not familiar with Android/Xamarin development. My guess is it's similar to how WinForms applications where background threads can't update the main UI thread. In those cases you have to have the data from the background thread made available to the main UI thread, and then it updates the UI elements itself. – Martin Costello Apr 09 '20 at 07:28