How to send a Post body in the HttpClient request in Windows Phone 8?

后端 未结 2 1667
广开言路
广开言路 2020-12-24 04:55

I have written the code below to send headers, post parameters. The problem is that I am using SendAsync since my request can be GET or POST. How can I add POST Body to this

2条回答
  •  醉梦人生
    2020-12-24 05:10

    This depends on what content do you have. You need to initialize your requestMessage.Content property with new HttpContent. For example:

    ...
    // Add request body
    if (isPostRequest)
    {
        requestMessage.Content = new ByteArrayContent(content);
    }
    ...
    

    where content is your encoded content. You also should include correct Content-type header.

    UPDATE:

    Oh, it can be even nicer (from this answer):

    requestMessage.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}", Encoding.UTF8, "application/json");
    

提交回复
热议问题