我在 C# 中有一个 WinForms 应用程序,我试图通过带有 HTTPS 协议的 GET 请求获取页面的 HTML 代码,但我无法获得任何值。
我尝试了多种解决方案,但我无法自行解决此问题。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Http;
using System.IO;
namespace sharpFUT
{
public partial class frmShapfut : Form
{
string HTMLCode;
public frmShapfut()
{
InitializeComponent();
}
private async void btnSearch_Click(object sender, EventArgs e)
{
try
{
HTMLCode = await getHTMLCode(tbxPlayerURL.Text);
}
catch (Exception)
{
MessageBox.Show(e.ToString());
}
MessageBox.Show(HTMLCode);
}
private async Task<string> getHTMLCode(string url)
{
var handler = new HttpClientHandler()
{
AllowAutoRedirect = true
};
HttpClient httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
var result = await httpClient.GetStringAsync(url);
return result;
}
}
}