所以我有一些代码可以访问网站的 html,因为它可以正常工作,但仅在 visualstudios.Framework for c# 中将此代码输入到 app.config 中时。
<system.net> <defaultProxy useDefaultCredentials="true" /> </system.net>
ps 之间的下一行 ><
但我需要这段代码在 .CORE 而不是 .FRAMEWORK 中工作 但它给出了这个错误。
System.Net.WebException:“远程服务器返回错误:(407) 需要代理身份验证。”
我尝试通过从解决方案资源管理器创建一个 app.config 来解决它,但没有影响它是否有效 互联网上的所有答案要么太复杂,要么表明我在 .FRAMEWORK 的 app.config 中放入了一点代码,但在 .Core 上不起作用
以代码为起点。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
Console.WriteLine("Sorry , somethings faulty");
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
readStream.ToString();
// Console.WriteLine(readStream);
}
string ImpureTexta = readStream.ReadToEnd().ToString();
BaseHTML = ImpureTexta; ///turns ImpureText in class to the actual html code so it can be used by entire program
Console.WriteLine(BaseHTML);
Console.WriteLine(" <--------------------------------Extraction B Complete --------------------------------->");
}
谢谢
最佳答案
.NET Core 没有app.config
文件。这需要在代码中配置。
使用 request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
关于c# - 如何解决 .NET.CORE 中的代理服务器 407 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59301307/