Unity: POST request using WWW class using JSON

后端 未结 4 1485
情话喂你
情话喂你 2021-01-07 01:18

I am trying to make a POST request to restful web APIs in Unity.

The header would be Content-Type: application/json

An example of the raw data i

4条回答
  •  心在旅途
    2021-01-07 01:49

    try {
        string url_registerEvent = "http://demo....?parameter1=" parameter1value"¶meter2="parameter2value;
    
        WebRequest req = WebRequest.Create (url_registerEvent);
        req.ContentType = "application/json";
        req.Method = "SET";
        //req.Credentials = new NetworkCredential ("connect10@gmail.com", "connect10api");
        HttpWebResponse resp = req.GetResponse () as HttpWebResponse;
    
        var encoding = resp.CharacterSet == ""
                    ? Encoding.UTF8
                    : Encoding.GetEncoding (resp.CharacterSet);
    
        using (var stream = resp.GetResponseStream ()) {
            var reader = new StreamReader (stream, encoding);
            var responseString = reader.ReadToEnd ();
    
            Debug.Log ("Result :" + responseString);
            //JObject json = JObject.Parse(str);
        }
    } catch (Exception e) {
        Debug.Log ("ERROR : " + e.Message);
    }
    

提交回复
热议问题