我有一个要求,比如...我想从 winforms 访问一个 url(登录页面是 web)。我必须将凭据传递给该 url,并且响应应该是经过身份验证的网页(标记)的内容。
我编写了一个函数,它将请求 url 并返回响应。但我收到错误代码(407)
“需要代理身份验证。”
这是我的代码。
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();
}