- Home /
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 + ":\nReceived: " + file);
Answer by xxmariofer · Mar 15, 2019 at 09:48 AM
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");
Dont works the download of xml file, i will try to use couroutines yielding the response of HttpWebRequest, mil gracias mario eres un crack!
really? i have tested and had no issues. you continue having the 403 forbidden message? try just creating a new cs script and put this code in the awake and check in case the error is somewhere else or there any typo error.
using (UnityWebRequest webRequest = UnityWebRequest.Get("http://www.mambiente.madrid.es/opendata/horario.xml"))
{
//if (actualPolution)
{
webRequest.SetRequestHeader("user-agent", @"$$anonymous$$ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb$$anonymous$$it/537.36 ($$anonymous$$HT$$anonymous$$L, 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( ": Error: " + webRequest.error);
}
else
{
string file = webRequest.downloadHandler.text;
Debug.Log(":\nReceived: " + file);
}
}
de nada! :)
Its working, my problem was that i was using the url http://www.mambiente.munimadrid.es/opendata/horario.xml ins$$anonymous$$d http://www.mambiente.madrid.es/opendata/horario.xml, Its the same xml but i think that the first one gave more problems. Thanks mario.
Your answer
![](https://koobas.hobune.stream/wayback/20220612193629im_/https://answers.unity.com/themes/thub/images/avi.jpg)