2015-02-09 105 views
0

我有一個問題,並試圖做到這一切!即使HttpUtility.ParseQueryString也無濟於事!Twitter API鏈接解析器

我試圖解析來自API的http://t.co/oEVQbihMWu形式的twitter鏈接。我需要完整解析的網址。

我的代碼:

richTextBox1.Clear(); 
      richTextBox1.Visible = true; 
      SearchOptions SO = new SearchOptions(); 
      SO.GeoCode = richTextBox3.Text + "," + richTextBox2.Text + "mi"; 
      TwitterResponse<TwitterSearchResultCollection> TweetSearchResult = TwitterSearch.Search(tokens, "#blogger", SO); 

      if (TweetSearchResult.Result != RequestResult.Success) richTextBox1.Text = "connection Error"; 
      else 
      { 
       string a = null; 

       foreach (var tweet in TweetSearchResult.ResponseObject) 
       { 
        string b = tweet.User.Location.Contains(",") ? tweet.User.Location.Replace(",", "-") : tweet.User.Location; 
        a += string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", tweet.CreatedDate, b, tweet.User.Id, 
         tweet.User.ScreenName, tweet.User.Name, tweet.User.NumberOfFollowers, tweet.User.Website, Environment.NewLine); 

        richTextBox1.AppendText(" " + tweet.CreatedDate + "\n" + tweet.User.Location + "\n" + tweet.User.Id + "\n" + tweet.User.ScreenName + "\n" + tweet.User.Name + "\n" + tweet.User.NumberOfFollowers + 
        "\n" + tweet.User.Website + "\n" + tweet.Text + "\n\n\n"); 



} 

鏈接被tweet.user.website表示。

有幫助嗎? :)

回答

0

在API響應中,有entities.urls其中包含urlexpanded_url映射的數組。檢查您的圖書館的文檔是否相同。

另外,如果你檢查了t.co鏈接的反應,你會發現這一點:

<noscript><META http-equiv="refresh" content="0;URL=http://www.fitnessbydanielle.com"></noscript><title>http://www.fitnessbydanielle.com</title><script>window.opener = null; location.replace("http:\/\/www.fitnessbydanielle.com")</script> 

解析它來獲取URL。

0

我設法破解它。 我做了什麼:

foreach (var tweet in TweetSearchResult.ResponseObject) 
       { 
        if(tweet.User.Website != null) 
        { 
         HttpWebRequest req = (HttpWebRequest)WebRequest.Create(tweet.User.Website); 
         req.AllowAutoRedirect = false; 
         var resp = req.GetResponse(); 
         string realUrl = resp.Headers["Location"]; 
         string b = tweet.User.Location.Contains(",") ? tweet.User.Location.Replace(",", "-") : tweet.User.Location; 
         a += string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", tweet.CreatedDate, b, tweet.User.Id, 
          tweet.User.ScreenName, tweet.User.Name, tweet.User.NumberOfFollowers, realUrl, Environment.NewLine); 

         richTextBox1.AppendText(" " + tweet.CreatedDate + "\n" + tweet.User.Location + "\n" + tweet.User.Id + "\n" + tweet.User.ScreenName + "\n" + tweet.User.Name + "\n" + tweet.User.NumberOfFollowers + 
         "\n" + realUrl + "\n" + tweet.Text + "\n\n\n"); 
        } 
       } 

       File.AppendAllText(@".\BloggerTable.csv", a, Encoding.UTF8); 

}

它包裹的條件內,從而沒有網站沒有用戶會顯示和使用的WebRequest來獲取鏈接。將每個推文的位置存儲在httprequest標頭內。