2017-06-20 55 views
0

即時通訊嘗試從網頁獲取一些數據。即時通訊編程在C#.net中。該網頁有一個下拉列表(或組合框),就像我下面顯示的那樣。數據根據所選下拉列表項目而改變,但url不會更改。我的問題是我的代碼如何更改選定的值並從網頁獲取數據?我分析,並得到了唯一的項目像一個:如何使用c#在html中更改選定的值?

 **WebClient wc = new WebClient(); 
     string kaynak = wc.DownloadString("http://www.diyanet.gov.tr/"); 
     string imsak = "spImsak"; 
     int imindex = kaynak.IndexOf(imsak); 
     imindex += 9; 
     System.Console.WriteLine(kaynak.Substring(imindex, 5));** 

<跨度ID = 「spImsak」> 02:44 </SPAN>

我下載網頁的HTML代碼作爲字符串。搜索「spImsak」。最後我得到了「02:44」作爲一個字符串。我想爲所有組合框項目做。你能給我什麼建議嗎?

樣本網頁:http://www.diyanet.gov.tr/

紅色的是組合框。黃色的是我想要得到的數據。

enter image description here

+0

爲了閱讀html字符串的片段,請看一下HtmlAgilityPack。然後你可以通過它的id找到該跨度。 –

+0

以這種方式下載HTML頁面時,您會從服務器獲取全新副本,而不是* * *瀏覽器中的任何副本。此外,它只是一個字符串,不知道任何關於組合框等 –

+0

@HansKesting我的公司不想使用第三方庫。 –

回答

0

我想接着網頁的網絡,發現當我點擊任何下拉列表元素,網頁與運行參數的Web服務。我解釋瞭如何將其應用於我的問題。

web service and parameters image

所有我需要發送POST請求這些參數這個Web服務,得到了串(JSON)。我做了以下c#代碼。

using (WebClient client = new WebClient()) 
     { 
      int turn; 
      byte[] response; 
      string result; 
      /* gets response for 81 city */ 
      for (turn = 500; turn < 581; ++turn) 
      { 

       response = 
       client.UploadValues("http://diyanet.gov.tr/PrayerTime/MainPrayerTimesSet", new NameValueCollection() 
       { 
        { "countryName", "2" }, 
        { "name", turn.ToString() } 
       }); 
       /* without sleep, web service does not response successive requests */ 
       System.Threading.Thread.Sleep(5); 

       /* turns incoming byte[] -> string */ 
       result = System.Text.Encoding.UTF8.GetString(response); 

       Console.WriteLine(result); 
      } 
     } 
相關問題