Web 请求 - 基础连接已关闭:连接意外关闭
Web Request - The underlying connection was closed: The connection was closed unexpectedly
当我 运行 在 Curl 中执行以下命令时,我得到了正确的预期结果:
curl -d "client_secret=VALUE" -d "client_id=VALUE" -d "username=VALUE" -d "password=VALUE" -d "grant_type=VALUE" "https://<host>/auth/realms/test-rai/protocol/openid-connect/token"
(同样的请求用Postman触发时,结果也是正确的。)
但是,当我从 C# 发出(相同的)请求时,我 运行 进入异常:
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientPost
{
class Program
{
static async Task Main(string[] args)
{
var url = "https://<host>/auth/realms/test-rai/protocol/openid-connect/token";
var values = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("client_secret", "VALUE"),
new KeyValuePair<string, string>("client_id", "VALUE"),
new KeyValuePair<string, string>("username", "VALUE"),
new KeyValuePair<string, string>("password", "VALUE"),
new KeyValuePair<string, string>("grant_type", "VALUE")
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12 |
SecurityProtocolType.Ssl3;
using (var httpClient = new HttpClient())
{
using (var content = new FormUrlEncodedContent(values))
{
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// Throws exception: An error occurred while sending the request. The underlying connection was closed: The connection was closed unexpectedly.
HttpResponseMessage response = await httpClient.PostAsync(url, content);
var result = await response.Content.ReadAsStringAsync();
}
}
}
}
}
抛出的异常是:
An error occurred while sending the request. The underlying connection was closed: The connection was closed unexpectedly.
在网上搜索时,我发现了很多关于设置 'ServicePointManager.SecurityProtocol' 的参考资料,我也这样做了。 (测试了所有选项。)
由于 Curl 和 Postman 都给出了 correct/expected 结果:我在这里缺少什么?
我自己找到了答案:
由于我的请求在 Postman 和 Curl 中按预期工作,我注意到 Postman 中有一个选项可以转换 Postman 请求转换成特定语言:在 'Save' 按钮下方,您会找到一个 'Code' 按钮来实现此目的。
我生成的 - 运行良好 - C# 代码是:
static void Main(string[] args)
{
var client = new RestClient("https://<host>/auth/realms/test-rai/protocol/openid-connect/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", "VALUE");
request.AddParameter("username", "VALUE");
request.AddParameter("password", "VALUE");
request.AddParameter("client_secret", "VALUE");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
当我 运行 在 Curl 中执行以下命令时,我得到了正确的预期结果:
curl -d "client_secret=VALUE" -d "client_id=VALUE" -d "username=VALUE" -d "password=VALUE" -d "grant_type=VALUE" "https://<host>/auth/realms/test-rai/protocol/openid-connect/token"
(同样的请求用Postman触发时,结果也是正确的。)
但是,当我从 C# 发出(相同的)请求时,我 运行 进入异常:
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientPost
{
class Program
{
static async Task Main(string[] args)
{
var url = "https://<host>/auth/realms/test-rai/protocol/openid-connect/token";
var values = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("client_secret", "VALUE"),
new KeyValuePair<string, string>("client_id", "VALUE"),
new KeyValuePair<string, string>("username", "VALUE"),
new KeyValuePair<string, string>("password", "VALUE"),
new KeyValuePair<string, string>("grant_type", "VALUE")
};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12 |
SecurityProtocolType.Ssl3;
using (var httpClient = new HttpClient())
{
using (var content = new FormUrlEncodedContent(values))
{
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// Throws exception: An error occurred while sending the request. The underlying connection was closed: The connection was closed unexpectedly.
HttpResponseMessage response = await httpClient.PostAsync(url, content);
var result = await response.Content.ReadAsStringAsync();
}
}
}
}
}
抛出的异常是:
An error occurred while sending the request. The underlying connection was closed: The connection was closed unexpectedly.
在网上搜索时,我发现了很多关于设置 'ServicePointManager.SecurityProtocol' 的参考资料,我也这样做了。 (测试了所有选项。)
由于 Curl 和 Postman 都给出了 correct/expected 结果:我在这里缺少什么?
我自己找到了答案:
由于我的请求在 Postman 和 Curl 中按预期工作,我注意到 Postman 中有一个选项可以转换 Postman 请求转换成特定语言:在 'Save' 按钮下方,您会找到一个 'Code' 按钮来实现此目的。
我生成的 - 运行良好 - C# 代码是:
static void Main(string[] args)
{
var client = new RestClient("https://<host>/auth/realms/test-rai/protocol/openid-connect/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", "VALUE");
request.AddParameter("username", "VALUE");
request.AddParameter("password", "VALUE");
request.AddParameter("client_secret", "VALUE");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}