9
我試圖做一個簡單的請求到Basecamp API,我遵循提供的說明添加樣本用戶代理和我的憑據,但我不斷收到403 Forbidden
迴應。HttpClient和設置授權標頭
我的憑證絕對正確,所以我的請求/憑證設置不正確?
這是我(刪除個人信息):
var httpClient = new HttpClient();
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("User-Agent", "MyApp [EMAIL ADDRESS]") });
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "[USERNAME]", "[PASSWORD]"))));
var response = await httpClient.PostAsync("https://basecamp.com/[USER ID]/api/v1/projects.json", content);
var responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
Console.WriteLine(await reader.ReadToEndAsync());
}
謝謝,不得不修改這一點,但得到它 – DGibbs
當時沒有編碼IDE,或許應該說是:) –
也請考慮爲什麼包裝客戶端成短命'using' _might_壞工作:HTTP: //stackoverflow.com/questions/22560971/what-is-the-overhead-of-creating-a-new-httpclient-per-call-in-a-webapi-client – mmlac