在c#中构建对休息服务器的POST请求

时间:2018-03-04 23:59:22

标签: c# rest post http-headers

我正在尝试根据以下文档构建对REST服务器的POST请求:

http://appsensor.org/docs/v2.3.0/api/ui/index.html#!/RestRequestHandler/resource_RestRequestHandler_addEvent_POST

我一直在使用我编写的以下代码:

    public async Task<HttpResponseMessage> events()
    {
        try
        {
            client.BaseAddress = new Uri("http://localhost:8085/");
        }
        catch (InvalidOperationException)
        { }
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("X-Appsensor-Client-Application-Name2", "myclientapp");
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "api/v1.0/events");
        request.Content = new StringContent("AE3",
                                            Encoding.UTF8,
                                            "application/json");//CONTENT-TYPE header


        HttpResponseMessage response = await client.PostAsJsonAsync("api/v1.0/events", request);

        System.Diagnostics.Debug.WriteLine(response.ToString());
        return response;
    }

但这是我得到的回应:

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: 
System.Net.Http.StreamContent, Headers:

{
  Connection: close
  Date: Sun, 04 Mar 2018 23:46:17 GMT
  Server: Apache-Coyote/1.1
  Content-Length: 402
  Content-Type: text/plain; charset=UTF-8
}

关于不正确的帖子请求有什么用?

0 个答案:

没有答案