HTTPReuqest really tanks the FPS

So right now I am making HTTP requests to a server, but I need to send a GET request with some JSON data as well. What I am doing right now "works", but is terrible because it drops down to nearly 15 FPS each request. Here is my code.

Here is my GET Query:

{
    "query": {
        "match_all": {}
    },
    "size": 1,
    "sort": [{
        "@timestamp": {
            "order": "desc"
        }
    }]
}

Here is my start method:

void Start()
{
        queryString = File.ReadAllText(Application.streamingAssetsPath + "/gimmeData.json");

        StartCoroutine(SetJsonData());


}

And my co routine that I am actually running to get the data:

 private IEnumerator SetJsonData()
    {
        // Make a new web request with the .net WebRequest
        request = WebRequest.Create(elk_url);
        yield return null;

        request.ContentType = "application/json";
        request.Method = "POST";
        buffer = Encoding.GetEncoding("UTF-8").GetBytes(queryString);

        // Put this yield here so that I get higher FPS
        yield return null;

        requestStream = request.GetRequestStream();
        requestStream.Write(buffer, 0, buffer.Length);
        requestStream.Close();
        // Again this is about getting higher FPS
        yield return null;

        response = (HttpWebResponse)request.GetResponse();
        // Wait until we have all of the data we need from the response to continue
        yield return requestStream = response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        reader = new StreamReader(requestStream);
        // Set my string to the response from the website
        JSON_String = reader.ReadToEnd();

        yield return null;

        // Cleanup the streams and the response.
        reader.Close();
        requestStream.Close();
        response.Close();
        yield return null;

        // As long as we are not null, put this in as real C# data
        if (JSON_String != null)
        {
            // Wait until we finish converting the string to JSON data to continue
            yield return StartCoroutine(StringToJson());
        }

        if(dataObject != null)
        {
            // Send the data to the game controller for all of our hits
            for(int i = 0; i < dataObject.hits.hits.Length; i++)
            {
                StartCoroutine(
                    gameControllerObj.CheckIpEnum(dataObject.hits.hits[i]._source));
            }
        }

        // As long as we didn't say to stop yet
        if (keepGoing)
        {
            yield return null;

            // Start this again
            StartCoroutine(SetJsonData());
        }
    }

I have all those "yield return null"s in there because it improved the FPS from what it already was.

How can I optimize this? Is there a better way to make a web request that can also send GET data with it? I know that UnityWebRequest is a thing, but that does not allow me to send the JSON data that I have.

How frequently is this being called? I’m not sure if it will make a difference in your FPS, but one critical thing I noticed here is that you’re using objects that implement IDisposable and they have handles to resources that make it important to dispose them (streams). You need to be using “using” statements around these to make sure they’re properly disposed or you’re going to run into some (likely serious) memory leaking issues.

You are using a WebRequest object. For the most part, its methods are synchronous (i.e., they will block the executing thread, the main thread of your game and hang your game). You will want to either offload what you’ve done to a background thread, or use the UnityWebRequest type which seems to do its work asynchronously by default.