2012-02-11 63 views
1

我試圖在我自己的應用程序中創建tinysong和的Grooveshark之​​間的相互作用搜索通過ID上tinysong.com

現在我真正想要的是能夠質疑一首歌曲的ID上tinysong,瑟如果它存在與否。

如果您搜索tinysong一首歌

,該webadress顯示你的歌是這樣

http://tinysong.com/#/share/aa/25062638

,其中25062638是ID的ID ..

我tryed創建的HttpWebRequest然後刮結果,看看ID是否有效,名稱是什麼,但它的Ajax調用,所以結果不顯示在httpwebrequest ..

我也看着螢火蟲(這不是很好在)和i看到那裏 有一個發佈請求與POSTDATA

答疑5B%5D = 25062638 &答疑5B%5D =搜索&答疑5B%5D至tinysong.com?s=sh = AA

其中第一個是ID第二是靜態的搜索,並且後者是SEARCHTEXT,其中我輸入(AA)

我已經tryed重新創建發佈請求與上述POSTDATA的路徑,但它返回頁腳??!

這是我寫的代碼:

string html = string.Empty; 


    string requestString = @"http://tinysong.com?s=sh"; 
    HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(requestString); 

    ASCIIEncoding encoding = new ASCIIEncoding(); 
    string postData = "q%5B%5D=4&q%5B%5D=search&q%5B%5D=aa"; 

    byte[] data = encoding.GetBytes(postData); 

    HttpWReq.Method = "POST"; 
    HttpWReq.ContentType = "application/x-www-form-urlencoded"; 
    HttpWReq.ContentLength = data.Length; 

    Stream newStream = HttpWReq.GetRequestStream(); 
    newStream.Write(data, 0, data.Length); 

    newStream.Close(); 


    // Create the web request 


    // Get response 
    using (HttpWebResponse response = HttpWReq.GetResponse() as HttpWebResponse) 
    { 
    // Get the response stream 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 

    // Console application output 
    html = reader.ReadToEnd(); 
    } 

我肯定失去了一些東西,所以任何幫助將appriciated。

回答

1

你的代碼看起來有點沉重。讓我建議你使用WebClient簡化:

using System; 
using System.Collections.Generic; 
using System.Collections.Specialized; 
using System.Net; 
using System.Text; 
using System.Web.Script.Serialization; 

class Program 
{ 
    static void Main() 
    { 
     using (var client = new WebClient()) 
     { 
      var values = new NameValueCollection 
      { 
       { "q[]", "25062638" }, 
       { "q[]", "search" }, 
       { "q[]", "aa" }, 
      }; 
      var result = client.UploadValues("http://tinysong.com?s=sh", values); 
      var json = Encoding.UTF8.GetString(result); 
      var serializer = new JavaScriptSerializer(); 
      var obj = (IDictionary<string, object>)serializer.DeserializeObject(json); 

      // there's also obj["html"] in the returned JSON 
      Console.WriteLine(obj["message"]); 

      // TODO: we have fetched the HTML, now you could scrape it in order to 
      // extract the information you are interested in. You could use 
      // an Html parser such as Html Agility Pack for this task: 
      // http://htmlagilitypack.codeplex.com/ 
     } 
    } 
} 
+0

謝謝達林,你的權利,這幫助了很多,但我有同樣的問題,如果你看一下HTML包含,它唯一的下半部的一部分的頁面出於某種原因,而不是實際的「歌曲數據」,我想刮... – user1204023 2012-02-11 19:32:58

+0

AHHHHHHHH我只是意識到,有一個HTML部分的JSON響應,也是一個消息部分,有我需要的部分,我只是用obj [「message」]替換了obj [「html」],我得到了我想要的,非常感謝Darin !!!! – user1204023 2012-02-11 19:38:10

+0

@ user1204023,我已經更新了我的回答以反映這一點。如果這篇文章幫助你解決了這個問題,你可以考慮[標記爲答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work),點擊它旁邊的勾號。 – 2012-02-11 21:00:42