本文介绍了通过HttpWebRequest类访问网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用httpwebrequest httpwebresonse class访问网页,但是我面临一个问题.
问题:当我访问网页时,响应是,我得到的是具有SSL证书(https://Loginpage.)的登录页面,而不是要求的页面,它没有绕过登录页面.


我的代码在下面..

Hi ,

I am accessing web page by using httpwebrequest and httpwebresonse class''s, but i am facing one issue.
Issue : When i am accessing the web page, the response, i am getting for Login Page which has SSL certificate(https://Loginpage.) not for requsted page, it is not bypassing login page.


My code is below..

string strurl = "http://Requstedpage";
        Uri uri = new Uri(strurl);
        ServicePointManager.ServerCertificateValidationCallback +=
delegate(
object sender1,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
    return true;
};


HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseText = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    responseText = reader.ReadToEnd();
response.Close();
string RespCookie = response.Cookies.ToString();
Response.Write(responseText);

推荐答案

public void ConfigureSSL()
{
    ServicePointManager.ServerCertificateValidationCallback = CertChecker;
}

private bool CertChecker(object sender, X509Certificate certificate,
    X509Chain chain, SslPolicyErrors errors)
{
    return certificate.Subject == "Certificate SSL Subject for your site";//
}



在第一个请求之前调用ConfigureSSL()方法.



Call ConfigureSSL() method before the first request.


这篇关于通过HttpWebRequest类访问网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:12