我想通過發送數據傳遞我的用戶名&密碼來登錄到控制檯的Https站點。但我無法做到這一點。我所得到的是處理'不正確的用戶名/密碼'情況的頁面。我搜索了很多,但無法解決這個問題。我已經成功地向經過的SSL,但仍然在錯誤的頁面..我共享的代碼我目前正在處理......登錄到SSL站點但失敗
CookieContainer CC = new CookieContainer();
WebRequest request = WebRequest.Create("https://the websit link is here!");
request.Proxy = null;
request.Credentials = CredentialCache.DefaultCredentials;
//allows for validation of SSL certificates
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
Console.WriteLine("\n");
Console.WriteLine("Enter your Email/Username :- ");
string username = Console.ReadLine();
Console.WriteLine("Enter your password :- ");
string password = Console.ReadLine();
string postData = String.Format("username={0}&password={1}", username, password);
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://the login page link of the website here! ");
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
// getRequest.CookieContainer.Add(getResponse.Cookies);
string responseString = new StreamReader(getResponse.GetResponseStream()).ReadToEnd();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
Console.WriteLine("Reader Object(StreamReader): " + reader);
string responseStr = reader.ToString();
string responseFromServer = reader.ReadToEnd();
Console.WriteLine("\nWriting to file...");
StreamWriter file = new StreamWriter("E:\\I'M Response_FinalTry.html");
try
{
for (int i = 0; i < responseFromServer.Length; i++)
{
file.Write(responseFromServer[i]);
}
}
catch (Exception e)
{
Console.WriteLine("Caught Exception is: " + e);
}
finally
{
Console.WriteLine("Closing file...");
file.Close();
Console.WriteLine("Done..!!");
}
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
我使用Stream類僅保存響應字符串/頁從服務器.. 我沒有服務器端代碼...... 請,引導我在這個問題.... :)
您是否嘗試過得到 'POSTDATA' 字節數組中UTF8和發送?另外,驗證您的內容類型。檢查:http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx –