0

I want to send and get data from my server using UnityWebRequest from unity 2107.4

I tried using code from here

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class WebRequestExample : MonoBehaviour
{
    // Where to send our request
    const string DEFAULT_URL = "https://jsonplaceholder.typicode.com/todos/1";
    string targetUrl = DEFAULT_URL;

    // Keep track of what we got back
    string recentData = "";

    void Awake()
    {
        this.StartCoroutine(this.RequestRoutine(this.targetUrl, this.ResponseCallback));
    }

    // Web requests are typially done asynchronously, so Unity's web request system
    // returns a yield instruction while it waits for the response.
    //
    private IEnumerator RequestRoutine(string url, Action<string> callback = null)
    {
        // Using the static constructor
        var request = UnityWebRequest.Get(url);

        // Wait for the response and then get our data
        yield return request.SendWebRequest();
        var data = request.downloadHandler.text;

        // This isn't required, but I prefer to pass in a callback so that I can
        // act on the response data outside of this function
        if (callback != null)
            callback(data);
    }

    // Callback to act on our response data
    private void ResponseCallback(string data)
    {
        Debug.Log(data);
        recentData = data;
    }

    // Old fashioned GUI system to show the example
    void OnGUI()
    {
        this.targetUrl = GUI.TextArea(new Rect(0, 0, 500, 100), this.targetUrl);
        GUI.TextArea(new Rect(0, 100, 500, 300), this.recentData);
        if (GUI.Button(new Rect(0, 400, 500, 100), "Resend Request"))
        {
            this.StartCoroutine(this.RequestRoutine(targetUrl, this.ResponseCallback));
        }
    }
}

and cs_getannounce , cs_login , cs_zonelist is the link that I tried to access

it should get data with all three links provided, but only cs_getannounce and cs_login can, while cs_zonelist cannot (get an error 403). I tried using unity 2018 too but no luck. When I use unity 2019, it works. But I really have to use 2017

derHugo
  • 83,094
  • 9
  • 75
  • 115
Imo
  • 25
  • 1
  • 6
  • All three [worked for me](https://imgur.com/a/BE1BtDQ) using an empty project with just your code and Unity `2017.4.31f1` – derHugo Aug 30 '19 at 06:22
  • @derHugo I cannot access imgur, please upload it somewhere else – Imo Aug 30 '19 at 06:26
  • Why can't you access it? Is it possible that in general your internet access is a bit strange? https://drive.google.com/open?id=1i0bsO77mL1c5jFmsQlondWyn3xeeG-6a – derHugo Aug 30 '19 at 06:28
  • @derHugo imgur blocked in my country :( – Imo Aug 30 '19 at 06:34

0 Answers0