Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jetese · Mar 15, 2019 at 09:26 AM · downloadwebrequesthttpheaders

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



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jetese · Mar 15, 2019 at 10:37 AM 0
Share

Dont works the download of xml file, i will try to use couroutines yielding the response of HttpWebRequest, mil gracias mario eres un crack!

avatar image xxmariofer jetese · Mar 15, 2019 at 10:45 AM 0
Share

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! :)

avatar image jetese xxmariofer · Mar 15, 2019 at 11:50 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sending Unity Web Request Post returning empty from webhost 1 Answer

Upload/download small files from internet in game? 1 Answer

Download progress is -1 forever. 2 Answers

How to check if DownloadHelperAudioClip still has internet connection for streaming during playback? 0 Answers

Not able to download the same file simultaneously 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges