如何使用WebClient忽略我的最终SSL证书?这是我目前正在使用的代码。
我的项目中没有看到ServicePointManager,为什么?我正在使用HttpCLient自动化一些API调用,我想忽略我的SSL。我正在使用.Net 4.5
static void Main(string[] args)
{
Task t = new Task(HTTP_GET);
t.Start();
Console.WriteLine("Downloading page...");
Console.ReadLine();
}
static async void HTTP_GET()
{
var TARGETURL = "https://myUrl";
using (HttpClient client = new HttpClient())
HttpClientHandler handler = new HttpClientHandler()
{
};
Console.WriteLine("GET: + " + TARGETURL);
// ... Use HttpClient.
HttpClient client = new HttpClient(handler);
var byteArray = Encoding.ASCII.GetBytes("user:pass");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null && result.Length >= 50)
{
Console.WriteLine(result.Substring(0, 50) + "...");
}
}
}
}
答案 0 :(得分:1)
这是答案:)希望有所帮助。
using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace WISetupAPI
{
class Program
{
static void Main(string[] args)
{
Task t = new Task(HTTP_GET);
t.Start();
Console.WriteLine("Downloading page...");
Console.ReadLine();
}
static async void HTTP_GET()
{
// ... Target page.
var TARGETURL = "https://Url";
using (var httpClientHandler = new HttpClientHandler())
{
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
using (var client = new HttpClient(httpClientHandler))
{
// Make request here.
Console.WriteLine("GET: + " + TARGETURL);
var byteArray = Encoding.ASCII.GetBytes("user:pass");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null && result.Length >= 50)
{
Console.WriteLine(result.Substring(0, 50) + "...");
}
}
}
}
}
}