0

I want to use POST method to pass data over web server. I want to retrieve data provided by web server in JSON format. I have used following code by referring Unity Manual.

 void Start ()
 {
     StartCoroutine (LoadLoginInfo ());
 }

 IEnumerator LoadLoginInfo ()
 {
     Debug.Log("Load Login Info");

     WWWForm form = new WWWForm ();
     form.AddField ("username", "admin");
     form.AddField ("password", "Admin123#");

     UnityWebRequest www = UnityWebRequest.Post (url, form);
     yield return www.Send();

     if (www.isError) {
         Debug.Log (www.error);
     } else {
         Debug.Log ("Data: " + www.downloadHandler.text);
     }
 }

But after certain amount of wait, I am getting following message in console.

enter image description here

If I try similar thing in browser then it works perfectly.

enter image description here

So what is the correct way to use POST method using WebRequest object?

Siddharth
  • 4,142
  • 9
  • 44
  • 90
  • Looks like cannot connect to the url. Try with chrome postman or some similar tool to check if can post to that address.. – mgear Jun 16 '16 at 08:28
  • Now please check one more time, I have edited my question. – Siddharth Jun 16 '16 at 08:32
  • 1
    just for testing, try with the old WWW also, the new unitywebrequest has had some issues still.. but the form+post looks fine. On the serverside you could dump the incoming request to text file to see if there is anything interesting (compared to the post from browser). – mgear Jun 17 '16 at 01:41
  • Using WWW object, I am facing caching problem. Many times I can't able to receive recent changed data. After some time, I automatically receive latest data at local mobile end. – Siddharth Jun 17 '16 at 09:42
  • on serverside you can set no caching? Or add random number to the url, like ?rnd=Random.float*999999; so then it wont get cached. – mgear Jun 17 '16 at 12:06
  • I don't know about server side variable regarding no caching but I talked with web developer regarding this. Second per time unique url strategy I know but I am working for iOS so this strategy will not work as per following discussion http://stackoverflow.com/questions/37608335/delay-in-accessing-recently-added-data-via-web-server – Siddharth Jun 18 '16 at 06:53
  • How to pass headers and arguments in UnityWebRequest post method?? – Siddharth Jul 16 '16 at 12:59

0 Answers0