gpt4 book ai didi

c# - 错误 (407) "Proxy Authentication Required."

转载 作者:行者123 更新时间:2023-11-30 13:59:04 27 4
gpt4 key购买 nike

我有这样的要求...我想从 winforms 访问一个 url(登录页面是 web)。我必须将凭据传递给该 url,响应应该是经过身份验证的网页(标记)的内容。

我编写了一个函数,它将请求 url 并返回响应。但我收到错误代码 (407)

"Proxy Authentication Required."

这是我的代码。

private static void GetPageContent(){
string url = "https://LoginPage.aspx/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
// If required by the server, set the credentials.
//request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.Credentials = new NetworkCredential("user1", "testuser#");
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}

最佳答案

WebProxy proxy = new WebProxy(proxyAddress);
proxy.Credentials = new NetworkCredential("username", "password", "domain");
proxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = proxy;

HttpWebRequest request = new HttpWebRequest();
request.Proxy = proxy;

或者您可以使用 WebClient

WebClient client = new WebClient();
client.Proxy = proxy;
string downloadString = client.DownloadString("http://www.google.com");

关于c# - 错误 (407) "Proxy Authentication Required.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com