c# - 如何解决 .NET.CORE 中的代理服务器 407 错误

标签 c# .net-core system.net.webexception webexception

所以我有一些代码可以访问网站的 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/

相关文章:

C# 终结器不释放非托管内存

.net-core - VS 2017 如何构建和测试包含 .Net Core 的混合解决方案

c# - 当结果为空时 LINQ 返回什么

c# - 将嵌套的 for 循环转换为单个 LINQ 语句

c# - C# 6.0 是否支持.NET Core,或者.NET Core 是否响应更高版本的C#?

c# - .NET Core 中的程序集加载

.net - System.Net.WebException:请求已中止:请求已取消

c# - 长时间运行后连接意外关闭 C#

c# - 使用 Nodejs 解压缩使用 C# 代码片段压缩的字符串