2014-01-13 88 views
0

我想通過發送數據傳遞我的用戶名&密碼來登錄到控制檯的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類僅保存響應字符串/頁從服務器.. 我沒有服務器端代碼...... 請,引導我在這個問題.... :)

+0

您是否嘗試過得到 'POSTDATA' 字節數組中UTF8和發送?另外,驗證您的內容類型。檢查:http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx –

回答

0

我相信你應該改變以下爲你的代碼工作:

getRequest.ContentType = "application/x-www-form-urlencoded"; 
byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

參見:http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx

編輯:

是否可以使用WebClient類呢?

using System.Net; 
using System.Collections.Specialized; 

using (var client = new WebClient()) 
{ 
    var values = new NameValueCollection(); 
    values["username"] = "user1"; 
    values["password"] = "12345"; 

    var response = client.UploadValues("https://www.mydomain.com/login.aspx", values); 

    var responseString = Encoding.Default.GetString(response); 
} 

來源:HTTP request with post

+0

謝謝,但實際上我已經應用此代碼,但問題是,它可以正常工作「臉譜。 com「和其他一些網站,但我希望它在」本地服務器「上工作。如大學自己的服務器。所以,這是主要問題。 – Zeeshan

+0

您是否使用該服務器的HTTPS?它可能沒有關係,但HTTPS證書是可信的嗎? –

+0

是的,它是HTTPS,我想通過編程方式登錄到它...我已經成功通過HTTPS旁路證書... – Zeeshan