Question by 
               Sephis · Aug 25, 2015 at 01:56 PM · 
                loginleaderboardwwwformposthttpwebrequest  
              
 
              How to stay connected on a website to send WWWForm?
I explain, so, I'm working on a game which is suppose to send score, pseudo, etc, to a web leaderboard. My problem is that I've two different URL, one for login (url/Account/Login) and the second to post my form (url/api/Post).
So, my question is, is it possible to login to the first URL and then send my form to the second?
I tried some things like that but nothing worked, I've got errors "500 Internal Server Error" or "401 unauthorized".
         IEnumerator SendForm(){
             WWWForm form = new WWWForm();
             Hashtable headers = form.headers;
             form.AddField ("null", "null");
             byte[] data = form.data;
             headers["Authorization"]="Basic " + System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("usernam:password")); 
             WWW w = new WWW(url+"Account/Login", data, headers);
             yield return w;
             if (!string.IsNullOrEmpty(w.error)) {
                 Debug.Log(w.error);
             }
             else {
                 Debug.Log(w.text);
                 form.AddField ("name", name);
                 form.AddField ("playerName", playerName);
                 form.AddField ("nationality", nationality);
                 form.AddField ("mobile", phone);
                 form.AddField ("points", GameManager.score.ToString());
                 WWW www = new WWW(url+"api/Scores", data, headers);
                 yield return www;
                 if (!string.IsNullOrEmpty(www.error)) {
                     Debug.Log(www.error);
                 }
                 else {
                     Debug.Log(www.text);
                 }
             }
         }
Thanks, Sephis
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Sephis · Aug 29, 2015 at 10:17 AM
If someone need it, if found a solution:
 using UnityEngine;
 using System;
 using System.Collections.Generic;
 using System.Collections;
 using System.Net;
 using MiniJSON;
  
 public class SendForm : MonoBehaviour {
     public string[] responseParsed = new string[];
  
     string     baseUrl = "https://www.company.com/game/";
     bool    scoreSubmitted = false;
  
     string bearerToken = string.Empty;
     WebClient client = new WebClient();
  
     //********** Login**************\\
     public IEnumerator Login () {
         client.BaseAddress = baseUrl;
  
         // Setup the request headers
         client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
         client.Headers.Add("X-Requested-With", "XMLHttpRequest");
  
         // Create the authentication request string
         var login = string.Format("grant_type=password&username={0}&password={1}", "username","password");
  
         UnsafeSecurityPolicy.Instate();
         string response = client.UploadString(new Uri(baseUrl+ "Token"), login);
        
         yield return new WaitForSeconds(0.1f);
         if(!string.IsNullOrEmpty(response))
         {
             responseParsed = response.Split(new char[]{'"'}); // I did this because Json.Deserialize didn't work
             bearerToken = responseParsed[3];
             Debug.Log(bearerToken);
             Array.Clear(responseParsed,0, responseParsed.Length);
         }
  
         if(!string.IsNullOrEmpty(bearerToken))
         {
             StartCoroutine(SubmitScore());
         } else Debug.Log("No bearer Token");
     }
     //************SUBMIT SCORE****************\\  
     public IEnumerator SubmitScore () {
         client.Headers.Add("Content-Type", "application/json; charset=UTF-8");
         client.Headers.Add("X-Requested-With", "XMLHttpRequest");
         client.Headers.Add ("Authorization", "Bearer "+ bearerToken);
        
         Dictionary<string, object> dict = new Dictionary<string, object>();
         dict.Add("name", "name");
         dict.Add("mobile", "mobile");
         dict.Add("playerName", "playerName");
         dict.Add("nationality", "nationality");
         dict.Add("points", GameManager.score);
         string data = Json.Serialize(dict);
         Debug.Log(data);
  
         string response = client.UploadString(new Uri(baseUrl+"api/Scores"), data);
         if(!string.IsNullOrEmpty(response))
         {
             scoreSubmitted = true;
             Debug.Log (response);
         }
         yield return data;
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                