如何在Webclient的发帖请求中发送x-www-form-urlencoded?

时间:2019-05-17 14:21:52

标签: c# webclient

我知道我可以发送json,但找不到如何发送x-www-form-urlencoded。我什么也找不到,所以不知道该怎么办。

WebClient wc = new WebClient();


string data = "channel_id=+12039273888&channel_type=phone&channel_verification=514950";

wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

string result = wc.UploadString("http://3.86.171.88/api/login", data);

System.Console.WriteLine(result);

2 个答案:

答案 0 :(得分:0)

您可以在UploadString()类上使用WebClient方法,例如

string data = "name=john&age=20&city=Uganda";
using (WebClient client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string result = client.UploadString(url of api resource, data);
}

答案 1 :(得分:-1)

        HttpClient client = new HttpClient();

        HttpContent content = new FormUrlEncodedContent(
            new List<KeyValuePair<string, string>>()
            );

        content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
        content.Headers.ContentType.CharSet = "UTF-8";
        client.DefaultRequestHeaders.ExpectContinue = false;
        HttpResponseMessage resposne = await client.PostAsync(new Uri(https://some url...), content);

希望这会有所帮助..因为HttpClient是最新的,所以也更好地使toawrds restapi精益化。.