WWW form "couldn't connect to host"

Hi people, I’ve been developing an interactive application at work, using a login system via php. I’ve setted up an Apache server on my machine, and the application works flawlessly. EXCEPT, when we try the app on other pc.

The logic is very simple, I send variables to a php script hosted internally, the php makes all the transactions with the Mysql database and print’s the results. Easy.

The problem is, that no matter if I set a local Apache server (Which can be accessed via lan), or a server with a public domain, when I make a STANDALONE exe for my application, it doesn’t connect.

At first, the php url was like http://192.168.1.13:8080/dev/unity/login.php , but when I noticed that other pc’s couldn’t connect to it VIA unity, I set it up on a public server like http://kevinseligmann.com.ar/dev/unity/login.php

The problem is the same, through unity editor it connects, but when making a standalone (NOT WEBPLAYER) it doesn’t.

Here’s my code:

function doLogin() {
	var sHash = Md5Sum(strUsername+strPassword+sYourID);
	var tURL = sURL;
	tURL=tURL+"?mdwCMD=loginRequest";
	tURL=tURL+"&user="+strUsername;
	tURL=tURL+"&pass="+strPassword;
	tURL=tURL+"&md5="+sHash;
	var rLogin = WWW(tURL);
	yield rLogin;
	switch(rLogin.text) {
		case "loginTrue":
			Application.LoadLevel (1);
			break;
		case "loginFalse":
			sLoginStatus = "Login Status: Failed, Please Try Again";
			yield WaitForSeconds(3);
			sLoginStatus = "";
			break;
	};
}

I also tried a simpler script, like:

function debugLogin(){
	var w:WWW = new WWW (sURL);
    yield w;
    Debug.Log (w.text);
}

But on the output log of the standalone player, it clearly says “Couldn’t connect to host”.

So, anyone has any idea what this can be?
Thanks in advance for your help.

That sounds like a malformed url, or inaccessibility to the host.

Also, check this link out as it may help you:
http://unity3d.com/support/documentation/Manual/Security%20Sandbox.html

The problem is that I can access and interact with the php script if I access it directly from the browser. Same URL that’s on my Js script.
And I’ve already checked about the sandbox, it only applies to Webplayer and mine is a Windows Standalone Exe.

:frowning:

I tried the same stuff ur talking about and I get through just fine. Try testing it on like a test.php with a generic feedback and see if you get the same error.

If so, give me a PM with the url to the test.php and I can have a look at it for you, just make sure you have the crossdomain.xml present on the root so that I can access it.

I tried with the crossdomain.xml and It’s the same. BUT I had a breakthrough. The url is set on a variable named sURL. I added another textbox to the scene, just to make it easy to try different domains without having to compile.

If I put on the textbox EXACTLY THE SAME URL that is stored on the sURL variable, it connects!.
I really don’t know what’s happening here, that’s why I’m going to post my entire code so you can see if you find something that’s not right, because I didn’t.

var sURL = "http://192.168.1.13:8080/unityLogin/login.php";
var sYourID : String = "d41d8cd98f00b204e9800998ecf8427e";

private var strUsername : String = "";
private var strPassword : String = "";
private var strLoginFile : String = "";
private var sBandwidthUsed : String = "";
private var sLoginStatus : String = "";

function Update() {
    sBandwidthUsed = "Streamed Bytes: " + Application.streamedBytes.ToString();
}

function OnGUI () {
	GUI.Label(Rect (10, 40, 200, 20), "Username: ");
	GUI.Label(Rect (10, 70, 200, 20), "Password: ");

    strUsername = GUI.TextField (Rect (100, 40, 200, 20), strUsername, 25);
    strPassword = GUI.TextField (Rect (100, 70, 200, 20), strPassword, 25);
    strLoginFile = GUI.TextField (Rect (100, 200, 200, 20), strLoginFile, 150);
    
	if (GUI.Button(Rect(10,100,50,30),"Login")) {
		doLogin(1);
	}
	if (GUI.Button(Rect(10,150,50,30),"Login Textbox")) {
		doLogin(2);
	}
	GUI.Label(Rect (10, 140, 400, 20), sBandwidthUsed);
	GUI.Label(Rect (30, 170, 400, 20), sLoginStatus);
}

function doLogin(asd : int) {
	var tURL;
	if(asd == 1){
		tURL = sURL;
	}else{
		if(asd == 2){
			tURL = strLoginFile;
		}
	}
	var sHash = Md5Sum(strUsername+strPassword+sYourID);
	
	tURL=tURL+"?mdwCMD=loginRequest";
	tURL=tURL+"&user="+strUsername;
	tURL=tURL+"&pass="+strPassword;
	tURL=tURL+"&md5="+sHash;
	var rLogin = WWW(tURL);
	yield rLogin;
	switch(rLogin.text) {
		case "loginTrue":
			Application.LoadLevel (1);
			break;
		case "loginFalse":
			sLoginStatus = "Login Status: Failed, Please Try Again";
			yield WaitForSeconds(3);
			sLoginStatus = "";
			break;
	};
}

static function Md5Sum(strToEncrypt: String) {
	var encoding = System.Text.UTF8Encoding();
	var bytes = encoding.GetBytes(strToEncrypt);
	var md5 = System.Security.Cryptography.MD5CryptoServiceProvider();	//-Encrypt bytes
	var hashBytes:byte[] = md5.ComputeHash(bytes);  					//-Convert the encrypted bytes back to a string (base 16)
	var hashString = "";
	for (var i=0; i<hashBytes.Length; i++) {
		hashString += System.Convert.ToString(hashBytes[i],16).PadLeft(2,"0"[0]);
	} return hashString.PadLeft(32,"0"[0]);
}

Check line 40 and 43. If the address is given by the sURL variable, it doesn’t work. But if I get it from the textbox strLoginFile works.

And I triple-checked if it was a typo when entering the address manually. It isn’t.

Note: When I discovered this, I switched again to a local server because it’s easy to manipulate the files.