【问题标题】:How to solve the Proxy Server 407 error in .NET.CORE如何解决 .NET.CORE 中的代理服务器 407 错误
【发布时间】:2020-04-05 15:37:54
【问题描述】:

所以我有一些代码可以访问网站的 html,因为它可以正常工作,但仅在 c# 的 visualstudios.Framework 中,当此代码输入 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 --------------------------------->");
        }

谢谢

【问题讨论】:

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


    【解决方案1】:

    .NET Core 没有 app.config 文件。这需要在代码中配置。

    使用request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

    【讨论】:

    • HttpWebRequest 用于进行 HTTP 调用的对象需要在调用 GetResponse() 之前设置上述属性。在 .NET Core 中,它不会像在 .NET Framework 中那样使用来自 app.config 文件的设置。
    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2020-01-21
    相关资源
    最近更新 更多