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 /
avatar image
0
Question by mortezabull · Aug 10, 2016 at 03:12 PM · android buildwww classcertificate

How to load a local key file using unity WWW class

Hi Guys,
I want to load a certificate using X509Certificate2 class. The code is:

 this.keyPath = Application.streamingAssetsPath + "/Key/AMCoIAKeyStore.PFX"
 theCertificate = new X509Certificate2(this.keyPath, keyPass)  

It works fine with the windows unity. But when I build it to Android, this Exception Occurs:

 I/Unity   ( 4317): DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/com.Test.ConferenceRoom3D-2/base.apk!/assets/Keys/AMCoIAKeyStore.PFX".
 I/Unity   ( 4317):   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
 I/Unity   ( 4317):   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess ac
 cess, FileShare share) [0x00000] in <filename unknown>:0
 I/Unity   ( 4317):   at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,Syst
 em.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
 I/Unity   ( 4317):   at System.IO.File.OpenRead (System.String path) [0x00000] in <filename unknown> :0  

It looks like, X509Certificate2 tries to use System.IO, and I read somewhere that System.IO can't retrieve data from jar files so I should use unity's WWW class to do it. Then i added a class to retrieve bytes of my certificate instead of passing the string of the path to X509Certificate2 (Becauese the X509Certificate2 has another constructor which works with byte array). The code is:

 using UnityEngine;
 using System.Collections;
 
 public class GetURL : MonoBehaviour {
     public byte[] key = null;
     public void startGetRequest(string url)
     {
         WWW www = new WWW(url);
         StartCoroutine(WaitForRequest(www));
     }
     IEnumerator WaitForRequest(WWW www)
     {
         yield return www;
         this.key = www.bytes;
         if (www.error == null)
         {
             Debug.Log("WWW Ok!: " + www.text);
         }
         else
         {
             Debug.Log("WWW Error: " + www.error);
         }
     }
  }

But now i'm facing a new error in windows unity:

 "Could not resolve host: D".  

The "url", is the path of my certificate. which in this case is:

 D:/Project/Assets/StreamingAssets/Key/AMColAKeyStore.PFX  

What did I wrong ? Is this the correct method to load a .pfx file in android builds ? Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SunnyChow · Aug 11, 2016 at 04:18 AM

try something like

new WWW ("File://"+Application.streamingAssetsPath + "/Key/AMCoIAKeyStore.PFX")

Comment
Add comment · Show 3 · 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
avatar image mortezabull · Aug 11, 2016 at 08:49 AM 0
Share

thank you for you answer. now it works in windows unity but what about the android ? now it throws an exception in android. the Application.strea$$anonymous$$gassets in android is: "/jar:file:/data/app/com.Test.ConferenceRoom3D-2/base.apk!/asset/$$anonymous$$eys/A$$anonymous$$CoIA$$anonymous$$eyStore.PFX".

avatar image SunnyChow mortezabull · Aug 12, 2016 at 02:26 AM 1
Share
 #if UNITY_ANDROID
 WWW www = new WWW(Application.strea$$anonymous$$gAssetsPath + "/$$anonymous$$ey/A$$anonymous$$CoIA$$anonymous$$eyStore.PFX");
 #else
 WWW www = new WWW("File://"+Application.strea$$anonymous$$gAssetsPath + "/$$anonymous$$ey/A$$anonymous$$CoIA$$anonymous$$eyStore.PFX");
 #endif

I think there should be a better way to do that.

avatar image mortezabull · Aug 13, 2016 at 12:11 PM 0
Share

thanks for your very helpful comments. But unfortunately, I'm facing a new problem. The Exception is:

Java.io.FileNotFoundException: assets/$$anonymous$$eys/A$$anonymous$$CoIA$$anonymous$$eyStore.PFX

The url in my device is: "jar:file:///data/app/com.Test.ConferenceRoom3D-2/base.apk!/assets/$$anonymous$$eys/A$$anonymous$$CoIA$$anonymous$$eyStore.PFX, and I'm just trying to read the .pfx file, which is a local file. And as I told you before, it works on my unity editor.

avatar image
0

Answer by shadyshrif · Jul 04, 2017 at 07:47 PM

this is my full code to read a text file

You have to put it inside your project in Assets/StreamingAssets/

   #if UNITY_ANDROID
        string  path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
          WWW wwwfile = new WWW(path);
          while (!wwwfile.isDone) { }
          var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
          File.WriteAllBytes(filepath, wwwfile.bytes);
  
          StreamReader wr = new StreamReader(filepath);
              string line;
              while ((line = wr.ReadLine()) != null)
              {
              //your code
              }
   #endif

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Acessing Steaming Assets folder in Android 1 Answer

How do I use Google Upload Key cert in my keystore 0 Answers

Download files via FTP on Android with WWW class 1 Answer

loading Images from streaming assets in Android 0 Answers

UTF-8 characters on Android build 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