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 liju_techtreeit · Dec 13, 2017 at 05:21 AM · wwwformauthenticationhttps

WWWForm HTTP POST And GET (Bad Request / Unauthorised) Issue

I am using WWWForm for HTTP GET and POST. In My Game URL credentials are added, and data GET and POST as Jsonformat.

I can get the details using credentials on Browser, But i cannot access details using WWWForm. First I was facing 401 Unauthorised problem. Then i added header like

 Dictionary<string, string> headers = form.headers;
      headers["content-type"] = "application/json"; 
      headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));



After that i don't have getting 401 Unauthorised problem . Know I have getting 400 Bad Request

Here is my Code.

 IEnumerator ConnectToServer2(string username,string password)
         {
             
             WWWForm form = new WWWForm();
     
             Dictionary<string, string> headers = form.headers;
             headers["content-type"] = "application/json"; 
             headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));
     
     //        form.AddField( "Book_Name", "Night%20at%20call%20centre" ); 
             form.AddField( "Book_Name", "Night at call centre" ); 
             form.AddField( "Book_Part", "First" );
             form.AddField( "Sequence", "1" );
     //        form.AddField( "username", username );
     //        form.AddField( "password", password );  
         
             byte[] rawData = form.data;
     
          
     
             // Post a request to an URL with our custom headers
             WWW www = new WWW(bookViewUrl, rawData, headers);  
             yield return www;
     
             if (www.error != null) {
                 Debug.Log (www.error);
             } else {
                 Debug.Log (www.text);
             }
             //.. process results from WWW request here...
         }

Thanks in advance.

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

Answer by liju_techtreeit · Dec 18, 2017 at 01:12 PM

 IEnumerator  LoginToServer (string username,string password) {
 //        Debug.Log ("LoginToServer "+username+" "+password);
         yield return new WaitForSeconds(0.1f);
         loadingCanvas.SetActive (true);
         yield return new WaitForSeconds(0.2f);
         Uri uri = new Uri (Global.loginUrl);
         Debug.Log ("ConnectToServer "+username+" "+password);
         HttpWebRequest requestGET = (HttpWebRequest)WebRequest.Create (uri);
         string xxx ="Basic "+ Convert.ToBase64String(Encoding.UTF8.GetBytes(username+":"+password));
         requestGET.Headers.Add ("authorization", xxx);
         requestGET.Method = "GET";  
         Debug.Log ("Before Response");
         bool isExceptionCame = false;
         yield return new WaitForSeconds(1f);
         try{
             var response = (HttpWebResponse)requestGET.GetResponse (); 
             Debug.Log ("After Response");
             StartCoroutine( WaitForResponse(response) );
         }catch(Exception e){
             Debug.Log ("Exception "+e.Message);
             if (e.Message.Contains ("UNAUTHORIZED") || e.Message.Contains ("Unauthorized")) {
                 ShowStatus (loginStatus [3]); 
             } else {
                 ShowStatus (loginStatus [4]);
             }
             isExceptionCame = true;
         }
         if (isExceptionCame) {
             yield return new WaitForSeconds (0.5f);
             loadingCanvas.SetActive (false);
             yield break;
         } else {
             yield return new WaitForSeconds (1f);
             //MyJsonParser ();
         }
     }

Is working for me

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

120 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 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 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 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 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

How to stay connected on a website to send WWWForm? 1 Answer

Not possible to cache assetbundles behind server authentication? 0 Answers

Google Play Services & Google Mobile Ads Together Issue 0 Answers

WWW HTTP Post request only works on Android if i'm comnected to Wifi, not on cellular data 1 Answer

Facebook SDK - iOS - Doesn't return user data 1 Answer


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