因此,我有一些代码,可以访问给定工作的网站的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 --------------------------------->");
}谢谢
发布于 2019-12-12 18:56:41
.NET核心没有app.config文件。这需要在代码中配置。
使用request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
https://stackoverflow.com/questions/59301307
复制相似问题