ниже приведен мой код (я добавил фиктивную конечную точку и учетные данные) < /p>
Код: Выделить всё
public class Program
{
static async Task Main(string[] args)
{
// Accept all SSL certificates (for testing purposes only)
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
// Enable TLS protocols
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12;
string url = "https://example/auth/token?grant_type=client_credentials";
string userId = "dummy";
string password = "dummypwd";
// Disable proxy
WebRequest.DefaultWebProxy = null;
var handler = new HttpClientHandler
{
UseProxy = false,
Proxy = null
};
using (var client = new HttpClient(handler))
{
string userCredentials = $"{userId}:{password}";
string basicAuth = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userCredentials));
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Add("Authorization", basicAuth);
// Optionally set Content-Type and content if required by the API
// request.Content = new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded");
try
{
var response = await client.SendAsync(request); //
Я получаю исключение на строке:
var response = await client.SendAsync(request);
В отличие
Код: Выделить всё
private SSLContext makeSslEnable() {
try {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
return sc;
} catch (Exception ex) {
log.info("Error configuring SSL for local test", ex);
return null;
}
}
public void sendPostRequest(String urlVals, String userId, String password) {
HttpsURLConnection conn = null;
BufferedReader in = null;
try {
URL url = new URL(urlVals);
conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
String userCredentials = userId + ":" + password;
String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
conn.setRequestProperty("Authorization", basicAuth);
SSLContext sslcontext = makeSslEnable();
if (sslcontext != null) {
conn.setSSLSocketFactory(sslcontext.getSocketFactory());
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
int statusCode = conn.getResponseCode();
String jsonContent = content.toString();
// Handle the response
} catch (Exception ex) {
log.info("Error during POST request", ex);
} finally {
try {
if (in != null) in.close();
if (conn != null) conn.disconnect();
} catch (Exception e) {
// Cleanup
}
}
}
< /code>
Я добавил заголовки и фрагменты кода от почты. Кроме того, я попытался добавить код servicepointmanager.servercertificatevalidationCallback = new RemoteCertificatificateValidationCallback (AcceptAllCertifications);
Подробнее здесь: https://stackoverflow.com/questions/796 ... n-in-net-f