使用 HttpClient returns 状态 463 的登录身份验证

login authentication with HttpClient returns status 463

我正在尝试连接到 API,该 API 为我提供了以下登录示例:

 POST /api/login  
 Host: nephele.qtestnet.com  
 Cache-Control: no-cache  
 Content-Type: application/x-www-form-urlencoded  
 j_username= maxmccloud%40qasymphony.com&j_password=p@ssw0rd  

我尝试将其实现为:

class QTestDriver
{
    HttpClient http = new HttpClient();
    public QTestDriver()
    {
        http.BaseAddress = new Uri("http://ahpra.testnet.com");
    }

    public async void Login()
    {
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/api/login");
        request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true };
        request.Content = new StringContent("j_username=scott&j_password=xxxx", Encoding.UTF8, "application/x-www-form-urlencoded");
        HttpResponseMessage resp = await http.SendAsync(request);
    }
}

但是我得到的回复是

"StatusCode: 463, ReasonPhrase: 'Unknown', Version: 1.0, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n  X-Cache: MISS from webtitan\r\n  Proxy-Connection: close\r\n  Date: Fri, 20 Mar 2015 05:04:32 GMT\r\n  ETag: \"5501c94b-19c4\"\r\n  Server: nginx/1.7.9\r\n  Via: 1.0 webtitan (squid/3.0.STABLE26)\r\n  Content-Length: 6596\r\n  Content-Type: text/html\r\n}"    string

我用谷歌搜索了这个,但找不到任何关于状态 463 的有用信息。

这是一个非常奇怪的回答,我强烈怀疑问题出在 API 供应商端。当您看到未在 Wikipedia page 中列出的状态代码(甚至包括 418(我是茶壶))时,可以肯定的是,它们肯定出了问题。

我没有发现您的代码有任何问题,但为了减少错误的余地,我会考虑使用 FormUrlEncodedContent instead of StringContent. You might even consider using my Flurl.Http 库,这会将整个调用减少为如下所示:

var resp = await "http://ahpra.testnet.com/api/login"
    .WithHeader("cache-control", "no-cache")
    .PostUrlEncodedAsync(new {
        j_username = "scott",
        j_password = "xxxx"
    });