Can t call my Web API using NET client in Raspberry Pi2 running Windows

0 votes

I'm running Windows 10 IoT Core on my Raspberry Pi 2 , and I need to make a call to my Web API using a C# client. Here's my code for it: 

private const string route = "/api/Print";
public bool Update(string header, string tc)
{
    bool success = false;
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("my uri");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var print = new Print { CompanyRef = new Guid(), Header = header, TC = tc };
        var response = client.PutAsJsonAsync(route, print);
    }
    success = true;

    return success;
}

public sealed class Print
{
    public string Header { get; set; }
    public string TC { get; set; }
    public System.Guid CompanyRef { get; set; }
}

And here's how I make the call to it:


Update(" header", " string tc");
 

While it works great in my C# Desktop App, it just won't work on the Pi. Although, it works fine when I call a Get from the Web API server. Please Help!

Aug 1, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
1,292 views

1 answer to this question.

0 votes

Try the following program. I have a similar code running and it works like a charm for me everytime.

    using Windows.Web.Http;
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
        try
        {
            var o = new
            {
                operation = "NewEvent",
                location_id = locationID,
                eventName = eventName
            };

            HttpStringContent content = new HttpStringContent(JsonConvert.SerializeObject(o), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

            HttpResponseMessage response = await httpClient.PostAsync(new Uri(urlPostData), content);
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            // TODO: Do something with the responseBody
        }
        catch (Exception)
        {
            // TODO: Deal with exception - could be a server not found, 401, 404, etc.
        }
    }

Hope this does it for you. :)

answered Aug 1, 2018 by nirvana
• 3,130 points

Related Questions In IoT (Internet of Things)

+1 vote
1 answer

Configuring Raspberry Pi with Windows 10 IoT App using Rest API

You should use HttpClient instead of WebClient. Try ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
916 views
0 votes
1 answer

PWA : Using Bluetooth in my phone in Progressive Web Apps

Yes, you can access in Geo Location(for ...READ MORE

answered Dec 20, 2018 in IoT (Internet of Things) by Shubham
• 13,490 points
6,482 views
0 votes
1 answer
0 votes
1 answer

Using Sleep()/Delay() in C# on Windows IoT

You'll be glad to know that C# ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
2,219 views
0 votes
1 answer

Setting time on Raspberry Pi using just PowerShell

The Enter-PSSession is for interactive sessions, so ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
946 views
0 votes
1 answer

Connecting GSM Module to Raspberry Pi 2

Hey, just use any 'supported' USB-to-Serial adapter ...READ MORE

answered Nov 22, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,019 views
0 votes
1 answer

Windows IoT Core : Boost C++

Since boost does not provide pre-built binaries ...READ MORE

answered May 8, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
1,016 views
0 votes
1 answer
0 votes
1 answer

Setting-up a RFID RC522 chip in Raspberry Pi?

First, let me congratulate you on buying ...READ MORE

answered Jul 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,643 views
0 votes
1 answer

Display time in a Windows Core IoT app with a clock!

It is possible, but you should understand ...READ MORE

answered Jul 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,539 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP