Ham*_*had 3 c# system.net.webexception webexception .net-core
所以我有一些代码可以访问网站的 html,但只能在 Visualstudios.Framework for c# 中将此代码输入到 app.config 中。
<system.net> <defaultProxy useDefaultCredentials="true" /> </system.net>
Run Code Online (Sandbox Code Playgroud)
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 --------------------------------->");
}
Run Code Online (Sandbox Code Playgroud)
谢谢
.NET Core 没有app.config文件。这需要在代码中配置。
使用request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
| 归档时间: |
|
| 查看次数: |
9442 次 |
| 最近记录: |