1

I have this code:

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; }
}

I call it like so:

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

In C# desktop app it works. In Windows 10 IoT on a Raspberry Pi2 device it does not work. Yet, when i am calling a Get from my Web API server *in Iot) it works fine. ?

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

1 Answers1

1

I am using this code for a year now and it works:

    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.
        }
    }
Makla
  • 9,899
  • 16
  • 72
  • 142