- Home /
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.
Answer by SunnyChow · Aug 11, 2016 at 04:18 AM
try something like
new WWW ("File://"+Application.streamingAssetsPath + "/Key/AMCoIAKeyStore.PFX")
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".
#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.
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.
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