Unity WWW and htaccess

Today I found out that htaccess and the WWW component from unity is not working on mobile device. You can’t call an URL with htaccess data in it!

I needed time to figure that out, cause htaccess works in editor of unity. I tested it with Android.
Example:

http://blah:12345@myurl.com

That’s not gonna work and will generate these exception:

java.io.FileNotFoundException

It’s not nice that we are limited like this in development. I don’t know exactly if it is a problem of java or unity or either both of them.
I hope this info will help other developers.

I’ve come across the same issue and have solved it by providing the Authorization header manually

//C#
public static class WWWHeaders
{
	public static string CreateAuthorization(string aUserName, string aPassword)
	{
		return "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(aUserName + ":" + aPassword));
	}
	
	public static Hashtable AddAuthorizationHeader(this Hashtable aHeaders, string aUserName, string aPassword)
	{
		aHeaders.Add("Authorization",CreateAuthorization(aUserName, aPassword));
		return aHeaders;
	}
}

This extention method for Hashtable can be used like this:

WWWForm form = new WWWForm();
Hashtable headers = form.headers.AddAuthorizationHeader("user", "pass");

WWW www = new WWW(URL, form.data, headers);

Note: You have to store a local copy of the headers Hashtable because form.headers only returns a temporary instance.

This works for me on Android. Haven’t tested iOS yet.

1 Like