我正在使用Basecamp API,这是一个REST(JSON)API,使用HTTPS进行基本的HTTP身份验证。
这应该是一个GET请求但是当我使用GET运行我的代码时,我收到了:
无法使用此动词类型
发送内容正文
当我将其作为POST运行时,我会收到:
{" status":" 400","错误":"错误请求"}
有谁知道为什么会这样?
using (var httpClient = new HttpClient()) {
string userName = "someone@someone.com";
string password = "somepassword";
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password)));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://correctUrlHere);
requestMessage.Headers.Add("User-Agent", "TheProject (someone@someone.com)");
requestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(requestMessage);
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
在这段代码中,我明显地换掉了用户名,密码,项目名称和URL,但在实际代码中它们都是正确的。
答案 0 :(得分:1)
GET请求必须将其参数作为url查询传递,而不是作为请求体传递。
http://example.com?p1=1&p2=helloworld
如果您没有任何内容,正如您的示例所示,请在请求中省略设置。
BadRequest结果表明您的有效负载出现了一些错误(同样:内容似乎是空的)。