CURL响应“200”,GetAsync“406”

时间:2017-03-15 23:59:23

标签: c# ruby-on-rails curl xamarin.forms

我正在尝试从rails api获取我的Xamarin表单应用程序的订单列表。

以下是我的Xamarin应用程序中返回“406”的代码:

async void UpdateOrders(string token)
    {
        // clear all the previous orders 
        orders.Clear();

        var client = new HttpClient();
        string url = "http://localhost:3000/api/orders?access_token=" + token;
        Debug.WriteLine(url);

        var response = await client.GetAsync(url);

        //response.EnsureSuccessStatusCode(); 

        if (!response.IsSuccessStatusCode)
        {
            Debug.WriteLine("this didn't work");
            //var ex = CreateExceptionFromResponseErrors(response);
            //throw ex;
        }
        else
        {
            // get the orders from the JSON 
            var orderItems = JsonConvert.DeserializeObject<List<Order>>(response.ToString());

            // add all the orders to the page 
            orders = new ObservableCollection<Order>(orderItems);
        }
    }

url =“http://localhost:3000/api/orders?access_token=03642494d1631421a8b49a21085b53907e8498794c0dcacc61c7b4eefbf1b7eb

**旁边注意CreateExceptionFromResponseErrors行是否会抛出错误?

当我在CURL中运行时,上面的同一个url返回以下内容:

[{"id":1,"invoice":"HALP","description":"TESTING","weight":100.0,"length":48,"width":48,"height":48,"account_id":1,"driver_id":null,"status":"preview","account_quote":576,"driver_quote":432,"created_at":"2017-03-11T17:18:40.418Z","updated_at":"2017-03-11T17:18:40.418Z","driver_rating":5,"account_rating":5,"guid":"cfd12c84-b260-440c-a21a-7a8aab44b5ac"}]

我哪里错了?

1 个答案:

答案 0 :(得分:0)

HTTP 406是&#34;不可接受&#34;的代码,这意味着您的HttpClient未正确配置为从服务器接收有效负载。您需要为正确的内容类型配置标头。

另见: What is “406-Not Acceptable Response” in HTTP?