Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Google play services 2 Answers

php $_POST is blank when I send variables from unity 3 Answers

POST JSON method does not work when project has been exported to WEBGL and upload to firebase,help please? 0 Answers

Why current culture is always en-US and what is the alternative ? 5 Answers

Having Trouble with Post to Server 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges