3

SO i have some code that accesses the html of a website given it woks but only in visualstudios.Framework for c# when this code is input into the app.config.

    <system.net> <defaultProxy useDefaultCredentials="true" /> </system.net>

ps next line between the ><

but i need this code to work in .CORE instead of .FRAMEWORK but it gives this error.

System.Net.WebException: 'The remote server returned an error: (407) Proxy Authentication Required.'

i tried solving it by creating a app.config from the solution explorer , but to no affect did it work all the answers on the internet are either too complicated or it shows that little bit of code i put in the app.config of .FRAMEWORK which does not work on .Core

the code as a starting point.

     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 --------------------------------->");
        }

Thanks

VillageTech
  • 1,968
  • 8
  • 18
Hamza Arshad
  • 121
  • 1
  • 3
  • 11

2 Answers2

5

.NET Core does not have app.config files. This needs to be configured in code.

use request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

Madushan
  • 6,977
  • 31
  • 79
  • `HttpWebRequest` object you use to make the HTTP call needs to have the above property set, before you call `GetResponse()`. In .NET Core, it will not use settings from an `app.config` file like in .NET Framework. – Madushan Dec 20 '19 at 07:16
  • This doesn't work for me, it needs " handler.UseDefaultCredentials= true;" to work for our proxy. – Ian Jowett May 06 '23 at 13:59
0

The following Error:

Message: The proxy tunnel request to proxy 'http://ipv4.xx.xxx.xxx.xx.webdefence.global.blackspider.com:8081/' failed with status code '407'."

Solved with the following NET Core code:

  HttpClientHandler handler = new HttpClientHandler();
            if (useProxy)
            {
                IWebProxy proxy = WebRequest.GetSystemWebProxy();
                proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                handler.UseDefaultCredentials= true;
                handler.Proxy = proxy;
            }

            using var httpClient = new HttpClient(handler);
Ian Jowett
  • 189
  • 18