Yasin T. Yasin T. - 3 years ago 127
C# Question

how to change selected value in html using c#?

im trying to get some datas from a webpage. im writing code in c# .net. the webpage has a dropdown list (or combobox) like i showed below. data changes according to selected dropdown list item but url does not change. my question is how can my code changes selected values and get datas from webpage? i parsed and got for only one of items like that:

**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));**

< span id="spImsak">02:44< /span >

i downloaded html code of webpage as a string. searched "spImsak". finally i got "02:44" as a string. and i wanna do it for all combobox items. can you give me any advice?

sample webpage : http://www.diyanet.gov.tr/

red ones are comboboxes. yellow one is data that i wanna get.

enter image description here

Answer Source

i have followed up webpage's network and saw that when i click any dropdown list element, webpage runs a web service with parameters. i explain how to applied that to my question.

web service and parameters image

all i need send a POST request this web service with these parameters and got string (json). i did that following c# code.

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 */

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download