- Home /
Send json to server C#
anyone know how to send json to server like this {"username":"hanaa123","password":"1234"}
and receive json like this { "status": "1" "name": "hanaa123" "statusMessage": "Login Successfully" "type": "user" "mode": "" "expireDate": "" }
server already have the configuration and works fine coz i tested it on my Android application (Android studio) but i don't know how to do the same with unity
Answer by phil_me_up · Feb 24, 2016 at 08:38 PM
You'll want to use the WWW class: http://docs.unity3d.com/ScriptReference/WWW.html
There are loads of examples of how to use this to both send and receive data so a quick search should sort you out.
Obviously if you're transmitting passwords, make sure your connections are using SSL.
Answer by hayabu4i · Feb 27, 2019 at 12:18 PM
private void SendJson(string url, string json)
{
StartCoroutine(PostRequestCoroutine(url, json, callback));
}
private IEnumerator PostRequestCoroutine(string url, string json)
{
var jsonBinary = System.Text.Encoding.UTF8.GetBytes(json);
DownloadHandlerBuffer downloadHandlerBuffer = new DownloadHandlerBuffer();
UploadHandlerRaw uploadHandlerRaw = new UploadHandlerRaw(jsonBinary);
uploadHandlerRaw.contentType = "application/json";
UnityWebRequest www =
new UnityWebRequest(url, "POST", downloadHandlerBuffer, uploadHandlerRaw);
yield return www.SendWebRequest();
if (www.isNetworkError)
Debug.LogError(string.Format("{0}: {1}", www.url, www.error));
else
Debug.Log(string.Format("Response: {0}", www.downloadHandler.text));
}
Your answer
Follow this Question
Related Questions
How do I send data to a cloud server like Amazon EC2? 1 Answer
Is there a library or package for HTTP communication with a server? 1 Answer
save and load json score to web/server 1 Answer
Upload json to cloud 0 Answers
binary serialization in c# 4 Answers