C# 在UWP上以Json格式发送请求

C# 在UWP上以Json格式发送请求,c#,json,uwp,dotnet-httpclient,azure-machine-learning-studio,C#,Json,Uwp,Dotnet Httpclient,Azure Machine Learning Studio,我已经用部署的web服务部署了AzureML发布的实验。我尝试使用,但通用应用程序尚未实现Http.Formatting,因此无法使用 我试着尽可能地遵循示例代码,但是我得到的状态代码是415“Unsupported Media Type”,我犯了什么错误 var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKe

我已经用部署的web服务部署了AzureML发布的实验。我尝试使用,但通用应用程序尚未实现Http.Formatting,因此无法使用

我试着尽可能地遵循示例代码,但是我得到的状态代码是415“Unsupported Media Type”,我犯了什么错误

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
// client.BaseAddress = uri;

var scoreRequest = new
{
            Inputs = new Dictionary<string, StringTable>() {
                    {
                        "dataInput",
                        new StringTable()
                        {
                            ColumnNames = new [] {"Direction", "meanX", "meanY", "meanZ"},
                            Values = new [,] {  { "", x.ToString(), y.ToString(), z.ToString() },  }
                        }
                    },
                },
            GlobalParameters = new Dictionary<string, string>() { }
 };
 var stringContent = new StringContent(scoreRequest.ToString());
 HttpResponseMessage response = await client.PostAsync(uri, stringContent);
var-client=new-HttpClient();
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,apiKey);
//client.BaseAddress=uri;
var scoreRequest=新
{
输入=新字典(){
{
“数据输入”,
新的StringTable()
{
ColumnNames=new[]{“方向”、“均值x”、“均值y”、“均值z”},
值=新[,]{,,,x.ToString(),y.ToString(),z.ToString()},}
}
},
},
GlobalParameters=新字典(){}
};
var stringContent=新的stringContent(scoreRequest.ToString());
HttpResponseMessage response=wait client.PostAsync(uri,stringContent);

非常感谢

您需要将对象序列化为JSON字符串(我建议使用NewtonSoft.JSON使其更简单),并相应地设置内容类型。下面是我在UWP应用程序中使用的一个实现(请注意,
\u client
是一个
HttpClient
):

公共异步任务同步(Uri,T项)
{
var itemAsJson=JsonConvert.serialized对象(item);
var content=新的StringContent(itemAsJson);
content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
返回wait_client.PostAsync(uri,内容);
}
    public async Task<HttpResponseMessage> PostAsJsonAsync<T>(Uri uri, T item)
    {
        var itemAsJson = JsonConvert.SerializeObject(item);
        var content = new StringContent(itemAsJson);
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return await _client.PostAsync(uri, content);
    }