我有一个要求,比如...我想从 winforms 访问一个 url(登录页面,即 Web)。我必须将凭据传递给该网址,并且响应应该是经过身份验证的网页(标记)的内容。
我已经编写了一个函数,它将请求 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();
}
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");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)