Open webpage programmatically and retrieve its html contain as a string

后端 未结 4 1827
梦毁少年i
梦毁少年i 2021-02-09 14:45

I have a facebook account and I would like to extract my friend\'s photo and its personal detail such as \"Date of birth\", \"Studied at\" and so on. I am able to extract the ad

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 15:01

    You have Three options:

    1- Using a WebClient object.

    WebClient webClient = new webClient();
    webClient.Credentials = new System.Net.NetworkCredential("UserName","Password", "Domain");
    string pageHTML = WebClient .DownloadString("http://url");`
    

    2- Using a WebRequest. This is the best solution because it gives you more control over your request.

    WebRequest myWebRequest = WebRequest.Create("http://URL");  
    WebResponse myWebResponse = myWebRequest.GetResponse();  
    Stream ReceiveStream = myWebResponse.GetResponseStream();                 
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 
    StreamReader readStream = new StreamReader( ReceiveStream, encode ); 
    string strResponse=readStream.ReadToEnd();                 
    StreamWriter oSw=new StreamWriter(strFilePath);     
    oSw.WriteLine(strResponse); 
    oSw.Close(); 
    readStream.Close();        
    myWebResponse.Close(); 
    

    3- Using a WebBrowser (I bet you don't wanna do that)

    WebBrowser wb = new WebBrowser();
    wb.Navigate("http://URL");
    string pageHTML = "";
    wb.DocumentCompleted += (sender, e) => pageHTML = wb.DocumentText;
    

    Excuse me if I misstyped any code because I improvised it and I don't have a syntax checker to check its correctness. But I think it should be fine.


    EDIT: For facebook pages. You may consider using facebook Graph API:

    http://developers.facebook.com/docs/reference/api/

提交回复
热议问题