我正在尝试通过 HttpWebRequest 访问 SSL 页面。看起来该页面使用了 JavaScript。我可以进入登录页面,但是在我登录后(我认为它已登录),我无法进入下一页。这是我的代码:
private void fetch(String password)
{
try
{
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
CookieContainer cookies = new CookieContainer();
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("https://facebook.com/");
http.CookieContainer = cookies;
http.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
IWebProxy proxy = http.Proxy;
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(http.RequestUri));
}
else
{
Console.WriteLine("Proxy is null; no proxy will be used");
}
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("myproxy");
myProxy.Address = newUri;
myProxy.Credentials = new NetworkCredential("username", password);
http.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
/////////////////////////////////////////////////////////
HttpStatusCode responseStatus;
responseStatus = response.StatusCode;
if (responseStatus == HttpStatusCode.OK)
{
UriBuilder urlBuilder = new UriBuilder("https://facebook.com/");
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
request.CookieContainer = cookies;
request.Referer = formUrl.ToString();
request.Method = "POST";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
using (Stream requestStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
{
request.PreAuthenticate = true;
ICredentials credentials = new NetworkCredential("username", password);
request.Credentials = credentials;
request.Method = "POST";
}
string responseContent = null;
using (HttpWebResponse response2 = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response2.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
responseContent = responseReader.ReadToEnd();
}
Console.WriteLine(responseContent);
response.Close();
response2.Close();
}
}
}
else
{
Console.WriteLine("Client was unable to connect!");
}
}
catch (UriFormatException e)
{
Console.WriteLine("Invalid URL");
}
catch (IOException e)
{
Console.WriteLine("Could not connect to URL");
}
}