0

Hi I am trying to implement a "PUT" request to send JSON string to my server, below is my code

public static string PUT (string url, string jsonStr){
    string msg = "";

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "PUT";
    ServicePointManager.Expect100Continue = false;

    using (StreamWriter sw = new StreamWriter(httpWebRequest.GetRequestStream())){
        sw.Write(jsonStr);
        sw.Flush();
        sw.Close();
    }

    try{
        HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse;
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            msg = streamReader.ReadToEnd();
        }
    }catch(WebException ex){
        msg = ex.Message;
    }

    return msg;
}

Right now I can receive msg from my server like "Success!" which is pretty short. But I wonder if this method should be written as a coroutine and do like "yield return response" or "yield return streamReader.EndofStream()" to ensure reading the response after all messages have been got from server.

I am using Unity3D which has its own web request method - "UnityWebRequest", it needs "yield return unityWebRequest.send()" to wait until request completed. That's why I think maybe HttpWebRequest also needs that.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
milanow
  • 159
  • 1
  • 13
  • You should not be using HttpWebRe* classes, you should be using the WWW class – Camilo Terevinto Jul 06 '16 at 20:01
  • @cFrozenDeath I have tried both UnityWebRequest and WWW, they don't work (still wondering why), that's why I use C#'s webRequest instead. – milanow Jul 06 '16 at 20:03
  • Your problem with WWW might be that it only accepts GET and POST requests – Camilo Terevinto Jul 06 '16 at 20:04
  • @cFrozenDeath Yes, that's the reason I gave up WWW, but actually UnityWebRequest can do all kinds of requests including PUT, DELETE, etc. But sometimes it works, sometimes not. It is still in UnityEngine.Experimental.Networking so I guess it is still in beta, not a stable one. – milanow Jul 06 '16 at 20:07
  • No, its not. Download Unity 5.4 and you will see that it has been moved out of UnityEngine.Experimental.Networking. – Programmer Jul 06 '16 at 20:09
  • @Programmer OMG Unity did remove it. Another reason not using it. – milanow Jul 06 '16 at 20:17
  • **"Another reason not using it**" Umm...They moved it to the permanent namespace which is `UnityEngine.Networking`. You can use it and if possible use it. – Programmer Jul 06 '16 at 20:27
  • @Programmer lol got it, thanks your answer! – milanow Jul 06 '16 at 20:47
  • You are welcome. If your question is answered, you can ahead and accept it as answer. http://i.stack.imgur.com/uqJeW.png You can do that to your other questions that are answered too. Happy coding! – Programmer Jul 07 '16 at 04:11
  • The use of WWW and UnityWebRequest, from what I can tell, is very inefficient if you make repeated calls and use SSL. The connection and hand shake over SSL takes up to 400ms and kills performance. From what I can tell neither classes uses or respects the `Connection: keep-alive` header. Something like this seems necessary as a result. – 10cool Jan 09 '17 at 23:19

1 Answers1

3

Does HttpWebResponse needs yield/coroutine to wait for message from server?

No.

But I wonder if this method should be written as a coroutine and do like "yield return response" or "yield return streamReader.EndofStream()" to ensure reading the response after all messages have been got from server.

You can use coroutine/yiled with WWW and UnityWebRequest. You can learn more about UnityWebRequest here.

For HttpWebResponse, you have to use it in another Thread or use the asynchronous version of it. Here is an example. If you don't do this, expect your game to be freezing most of the time, especially when you are receiving large data.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I am considering this one, it seems exactly what I want. [here](http://stackoverflow.com/a/12606963/3577115) – milanow Jul 06 '16 at 20:19
  • @milanow Good. Use any that works for you. As long as this answers your question that you can't use `yield coroutine` with `HttpWebResponse`. – Programmer Jul 06 '16 at 20:28