- Home /
Question by
TechGYQ · Oct 06, 2019 at 02:26 AM ·
c#unity 5downloadwebrequest
Web request pulls html script instead of file text!
Having an issue in unity web requests. When I try to download a file from my url if it's not included or if I've updated it online I always get this as a return if I use web client, web request or any variation.
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD
> HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
>
> <head> <title>MythosWorks</title>
> <meta name="description" content="">
> <meta name="keywords" content="">
> </head> <frameset rows="100%,*"
> border="0"> <frame
> src="http://98.30.237.126/repo/amb/words.json"
> frameborder="0" /> </frameset>
>
> </html>
I just want to return the text form the json file. How would I go about that this is my unity script currently.
> > using System.Collections; using System.Collections.Generic; using
> System.IO; using System.Net; using
> UnityEngine; using
> UnityEngine.Networking;
>
> public class VersionControl :
> MonoBehaviour {
> // Start is called before the first frame update
> void Start()
> {
> VersionCheck();
> }
>
> // Update is called once per frame
> void Update()
> {
>
> }
>
> // Loading Game Check from dictionary file > version > build!
> private void VersionCheck()
> {
> // Check if file exists!
> if (File.Exists(Application.dataPath +
> "/Scripts/DB/words.json"))
> {
> // Set file path & create reader
> string path = Application.dataPath +
> "/Scripts/DB/words.json";
> using (StreamReader dfetch = new StreamReader(path))
> {
> // fetch data from file and build list, then provide word
> count!
> string json = dfetch.ReadToEnd();
> jsonDataClass jsnData = JsonUtility.FromJson<jsonDataClass>(json);
> }
> }
> // Fetch file form repo!
> else
> {
> WebClient moogle = new WebClient();
> moogle.Headers["User-Agent"] =
> "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36
> (KHTML, like Gecko)
> Chrome/45.0.2454.85 Safari/537.36";
> moogle.DownloadFile("http://www.mythosworks.com/repo/amb/words.json",
> Application.dataPath +
> "/Scripts/DB/words.json");
> Debug.Log("Working on download");
> VersionCheck();
> }
> }
Tried without the headers as well. Don't matter if I have to parse the data down, but just really want to download the exact file and save it.
Comment
Best Answer
Answer by Mouton · Oct 06, 2019 at 03:05 PM
You need to change how you serve the static file from the server. In your server application, you need to serve the file with content-type
header set to application/json
then serve the file directly as a JSON entity.