1

I am currently developing in Unity (in particular using C#) and I'm stuck with HttpWebRequest - HttpWebResponse random timeouts.

I have some methods that send a POST request to a server I host on my local machine (XAMPP) to use various php scripts which are going to fetch informations from MySQL Database (hosted with XAMPP) and give back those info in JSON format.
Then I handle these JSON informations with my C# scripts.

The problem is that when I run the first test all is good:I can get the JSON data from my Server and show it in the Debug Console.
When I run the second test,a WebException is raised with error:

WebException - The request timed out

After that second test,if I run again and again,the problem keeps presenting in a random way.
I followed all the guidelines I found on the internet on how to setup a webrequest - webresponse properly,in particular I tried to use ServicePoint.DefaultConnectionLimit and ServicePoint.MaxServicePointIdleTime,without any result. The general structure of my methods (regarding the web request/response part) is something like that:

public void WebMethod(){
    string post_url = "http://localhost/service.php?someparam=1&someparam=2";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(post_url);
    request.Method = "POST";
    request.KeepAlive = false;
    request.Timeout = 5000;
    request.Proxy = null;

    string Response = "";
    try
    {
        using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
        {
            using (Stream objStream = resp.GetResponseStream())
            {
                using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8))
                {
                    Response = objReader.ReadToEnd();
                    objReader.Close();
                }
                objStream.Flush();
                objStream.Close();

            }

            resp.Close();

        }
    }catch(WebException e)
    {
        Debug.Log(e.Message);

    }
    finally
    {

        request.Abort();
    }

    //tried this one after reading some related answers here on StackOverflow,without results
    //GC.Collect();

    Debug.Log("SERVER RESPONSE:" + Response);

    //Response Handling
    }

I know that it may be something related to a wrong abort on the HttpWebRequest / Response or maybe related to the HTTP 1.1 connections limit,but I can't figure out any solution at the moment.
Any help is appreciated.

aldoalpha
  • 163
  • 10
  • The question that immediately comes to mind is why you're using `HttpWebRequest` instead of the Unity class [WWW Class](https://docs.unity3d.com/ScriptReference/WWW.html) ? Is there a specific reason for this choice? –  Jun 10 '16 at 11:21
  • I started recently to develop in C# / Unity,and I saw that using the WWW class requires IEnumerator as a return type. I got some design-class to realize,and my first thought was to implement them this way. Do you think that WWW class does not present the issue I'm facing with HttpWebRequest? – aldoalpha Jun 10 '16 at 11:27
  • 1
    I am not sure about that, but it is worth a try. There wouldn't be a reason for Unity to roll their own implementation of HttpRequests. Regarding IEnumerator, you need to start a Coroutine to use it. You should learn using Coroutines for Unity, because you will need it, for example for WebRequests as you can see. (or script based animation etc.) –  Jun 10 '16 at 11:33
  • check this: http://stackoverflow.com/questions/5827030/httpwebrequest-times-out-on-second-call – joreldraw Jun 10 '16 at 11:45

0 Answers0