我正在尝试使用.NET
执行以下操作如果我的用户名为test
且密码为password
,那么使用HttpClient时C#代码会是什么样的?
HTTP Method: GET
URL: http://webapi.ebayclassifieds.com/webapi/categories
Sample command:
curl --digest -u{username}:{password} http://webapi.ebayclassifieds.com/webapi/categories
这是我所拥有的,但我没有得到html:
var client = new HttpClient();
var requestContent = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("-u", "{test}:{password}") });
HttpResponseMessage response = await client.PostAsync(
"http://webapi.ebayclassifieds.com/webapi/categories", requestContent);
// Get the response content.
HttpContent responseContent = response.Content;
var blah = response.Content.ReadAsStringAsync();
答案 0 :(得分:1)
试试这个:
CredentialCache credCache = new CredentialCache();
credCache.Add (new Uri ("http://webapi.ebayclassifieds.com"), "Digest", new NetworkCredential ("username", "passwd"));
之后使用凭证缓存构建HttpClient
:
var httpClient = new HttpClient( new HttpClientHandler { Credentials = credCache});
其余的过程是一样的。如果您执行http POST
,请使用PostAsync
,对于http GET
,请使用GetAsync
。
您可以从CredentialCache找到有关here的更多详细信息。