2011-06-04 93 views
0

我花了太多的時間到現在沒有成功未能登錄到遠程站點

從我的網站這是在MVC IM開發嘗試發送登錄請求到遠程站點解決這個問題,直到,例如,臉書。

從提琴手似乎需要以下輸入charset_test,lsd,語言環境,電子郵件,通過。

LSD關鍵似乎是唯一令牌 這裏是我的代碼

CookieContainer cookies = new CookieContainer() 
private string GetToken() 
{ 
    Uri uri = new Uri("http://www.facebook.com"); 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
    request.CookieContainer = cookies; 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

    Stream stream = response.GetResponseStream(); 
    StreamReader reader = new StreamReader(stream); 

    string htmlText = reader.ReadToEnd(); 

    HtmlDocument document = new HtmlDocument(); --> HtmlAgilePack object 
    document.LoadHtml(htmlText); 
    //Need to check xpath doesn't return null object 
    HtmlNode node = document.DocumentNode.SelectNodes("//input[@name='lsd']").First(); 

    return node.Attributes["Value"].Value; 
} 

private void Login() 
{ 
    string postData = string.Format("charset_test=fixcharset_test&lsd={0} &locale=en_US&email=myemail&pass=mypass", HttpUtility.UrlEncode(GetToken())); 

    Uri uri = new Uri("http://www.facebook.com/login.php"); 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
    request.CookieContainer = cookies;  
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.ContentLength = postData.Length; 
    request.Method = "POST"; 
    request.AllowAutoRedirect = true; 
    StreamWriter requestWriter = new StreamWriter(request.GetRequestStream()); 
    requestWriter.Write(postData); 
    requestWriter.Close(); 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
} 

//After login to facebook suppose to 
public string void AddFriend() 
{ 
    Uri uri = new Uri("http://www.facebook.com"); 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
    request.CookieContainer = cookies; 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    string status = response.StatusDescription; 
    StreamReader responseReader = new StreamReader(response.GetResponseStream()); 
    string responseData = responseReader.ReadToEnd(); 
    responseReader.Close(); 
    return responseDat 
} 

請記住,Facebook是隻是一個例子而言,我意識到它的API的,我試着登錄到網站,沒有API 。 不幸的是,響應返回一個Facebook頁面,並帶有遺憾的消息,無法識別我的瀏覽器。

任何幫助將超過欣賞。

謝謝,

+0

您似乎忘記提問了。 – Oded 2011-06-04 21:26:06

+0

我的問題是我在代碼中錯過了什麼:) – 2011-06-04 21:47:42

+0

是否有另一種方式將所需的表單輸入提交給遠程網站? – 2011-06-04 22:00:18

回答

0

您是否在您的請求上設置HTTP_USER_AGENT?

+0

是的,我確實,request.UserAgent =「Mozilla/4.0(兼容; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; InfoPath.1)「;根據提琴手 – 2011-06-04 21:50:37