cURL POST到HttpClient

时间:2018-06-11 21:54:58

标签: c# http curl post httpclient

我有一个API提供的以下链接,它应该用于获取身份验证令牌:

curl -d "client=YourAppName&accountType=HOSTED_OR_GOOGLE&service=reader&Email=test@krasnoukhov.com&Passwd=..." https://theoldreader.com/accounts/ClientLogin

当我输入此行进入linux控制台时(显然我用自己的凭据)我得到了预期的结果:

SID=none
LSID=none
Auth=AnyKindOfKey12345

但是我需要在C#中使用它,所以我尝试使用HttpClient类如下:

var client = new HttpClient();
string url = "https://theoldreader.com/accounts/ClientLogin";
string request = "client=YourAppName&accountType=HOSTED_OR_GOOGLE&service=reader&Email=test@krasnoukhov.com&Passwd=...";
var response = await client.PostAsync(url, new StringContent(request));

但我每次都会获得状态代码“403 - Forbidden”。我错过了什么? 我尝试将StringContent的编码设置为UTF-8或ASCII,但没有更改。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情怎么样?我不认为你目前这样做的方式是正确的。

        string url = "https://theoldreader.com/accounts/ClientLogin";
        var client = new HttpClient();

        var urlData = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("client", "YourAppName"),
            new KeyValuePair<string, string>("accountType", "HOSTED_OR_GOOGLE"),
            new KeyValuePair<string, string>("service", "reader"),
            new KeyValuePair<string, string>("Email", "test@krasnoukhov.com"),
            new KeyValuePair<string, string>("Passwd", "pancakes")
        };

        var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(urlData) };

        var res = await client.SendAsync(request);