- Home /
See Request of WWW or UnityWebRequest, 403 error only from android in a specific url (GET)
Hello, I have a code that downloads data from a url. I have a problem that one Url gives me error 403 (Forbidden) only from android. When i get the url from editor or Windows build, all works perfectly. So if i could see the Request that WWW or UnityWebRequest does from android and Windows i would be able to compare whats going wrong.
I tested it with two codes ( WWW and UnityWebRequest) and both give the same result. i tested it with other urls that contains xml, and it works.
Url: http://www.mambiente.madrid.es/opendata/horario.xml
WWW
IEnumerator DownloadXml(string url, DateTime data = new DateTime())
{
WWW www = new WWW(url);
yield return www;
ParseXML text = new ParseXML();
string file = www.text;
UnityWebRequest
IEnumerator GetRequest(string url, DateTime data, bool actualPolution)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// 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 14, 2019 at 10:24 AM
after some test i have managed to retrieve the server data, had to add the accept info this code is tested and works for that site: (i have found a lot of different issues with sites from the Comunidad de Madrid)
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";
webRequest.CookieContainer = cookies;
XmlDocument xmldoc = new XmlDocument();
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
{
string xml = streamReader.ReadToEnd();
xmldoc.LoadXml(xml);
}
}
It works perfectly in android. I will try to adapt my code with this new information because i dont want to have two differents codes for the same task. If i dont get it i will use yours.
Thank you xxmariofer for your time.
Answer by misher · Mar 14, 2019 at 09:55 AM
You should debug your android app, use logcat (for example, in Android Studio).
I suspect it to be related to the CORS problems. Make sure your server respond with: Access-Control-Allow-Origin:*
header