why does my web api client call not work in Raspberry Pi2 Iot

China☆狼群 提交于 2019-12-24 13:52:38

问题


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. ?


回答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.
        }
    }


来源:https://stackoverflow.com/questions/33464906/why-does-my-web-api-client-call-not-work-in-raspberry-pi2-iot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!