Convert code from HttpWebRequest to UnityWebRequest (SetRequestHeader)

Hello,

I have problems downloading a xml from a specific url. Then someone of this forum solved me the problem with HttpWebRequest. Http solution

I need to convert it because i want to use coroutines and i cant do it with HttpRequest.

I tried to convert the code, but when i do it, it keeps failing. Whats wrong with my code?

HttpWebRequestCode

string xml;
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.mambiente.madrid.es/opendata/horario.xml");
        webRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
        webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
        webRequest.Method = "GET";
        XmlDocument xmldoc = new XmlDocument();
        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                xml = streamReader.ReadToEnd();
                xmldoc.LoadXml(xml);
            }
        }

My try to convert it

using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
        {

            if (actualPolution)
            {
                webRequest.SetRequestHeader("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");
                webRequest.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
               
            }
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            if (webRequest.isNetworkError)
            {
                Debug.Log(url + ": Error: " + webRequest.error);
            }
            else
            {
                ParseXML text = new ParseXML();
                string file = webRequest.downloadHandler.text;
                Debug.Log(url + ":

Received: " + file);

hello! continuing with the previous post. you can use HttpWebRequest with corroutines there is no limitation, but in case you want to use the Unity request api you need to use the right header that you can find Here (avoid caps :). tested but in case there is any issue tell me!) Code example:

webRequest.SetRequestHeader("user-agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36");

webRequest.SetRequestHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");