x


How to POST and GET on a https web page

I've been using the example code in the unity documents to send and get high score data from a php file on a server. I use the header to store the authorization key to access that file and it works a charm.

Now I tried the same thing for a different php file for the login server and it doesn't work. The authorization key is correct because I've tested it in a browser and it returns some information but in-game the server just times out waiting for a POST request.

The server admin claims that there have been no access attempts logged so it must be stumbling right after accepting the authorization key, if I remove the secret key then the server returns straight away with an unauthorized access message.

Has anyone else had this problem? Is there another way to POST or GET on a server with or without using the unity WWW class.

I've tried using unity 3 and have changed www.data to www.text but it still doesn't work. Is there any new functions on the latest WWW class?

This is for iPhone if it makes any difference.

Code:

var state : LoginState;
var secretKey : String = "user:mySecretKey";

function Login(USERNAME : String, PASSWORD : String){   
    // Create a Web Form
        var form = new WWWForm();
        form.AddField("Username", USERNAME);
        form.AddField("Password", PASSWORD);

    var headers=form.headers;
    var rawData=form.data;

    print("Username : " + USERNAME + " Password : " + PASSWORD);

    // Add a custom header to the request.
    // In this case a basic authentication to access a password protected resource.
    headers["Authorization"]="Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(secretKey));

    // Post a request to an URL
    var www = new WWW(validateURL, rawData, headers);
    yield www;

    if(www.error) {
        print("There was an error posting the high score: " + www.error);
        state = LoginState.loginFailed;
    }else{
        print(www.text);
        if(www.text[0] == "<"){
            state = LoginState.timedOut;    
        }else{
            state = LoginState.loginSuccess;
        }
    }
}
more ▼

asked Aug 01 '10 at 03:33 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 92

Just curious if you got this to work? Also have you tried it on PC/OSX and the results were?

Oct 11 '12 at 10:36 AM codeoverload
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

UnityWeb might help you. It now supports HTTPS, and can do custom GET POST HEADERS the works. :-)

more ▼

answered Mar 03 '11 at 02:42 AM

Simon Wittber gravatar image

Simon Wittber
323 5 6 14

Yup, looks very interesting. Will try that too.

May 22 '11 at 11:26 PM BerggreenDK
(comments are locked)
10|3000 characters needed characters left
function postScore(name, score) {
    //This connects to a server side php script that will add the name and score to a MySQL DB.
    // Supply it with a string representing the players name and the players score.
    var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score;

    // Post the URL to the site and create a download object to get the result.
    hs_post = WWW(highscore_url);
    yield hs_post; // Wait until the download is done
    if(hs_post.error) {
        print("There was an error posting the high score: " + hs_post.error);
    }
}

This is something I'm using atm, works a treat. Of course there's also a lot of additional checking etc in headings for security etc.

more ▼

answered Sep 07 '10 at 01:06 PM

jtbentley gravatar image

jtbentley
579 3 4 16

The security bit is the important part, the above code works for every site except this one as it has an odd login requirement. I used the same code for a different website with a login and it worked. I'm half certain it's to do with the server side but I'm curious if there's alternative client methods.

Sep 07 '10 at 10:15 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

Saw this on another answers page. You can't modify a WWWForm's headers directly, you have to create your own:

(C#)

var form = new WWWForm();
form.AddField( "SomeKey", "SomeValue" );

var headers = new Hashtable();
headers["SomeHeaderKey"] = "SomeHeaderValue";

var www = new WWW( url, form.data, headers );
more ▼

answered Dec 21 '12 at 02:11 AM

3j- gravatar image

3j-
56 3 3 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x548
x76
x36
x14

asked: Aug 01 '10 at 03:33 PM

Seen: 13919 times

Last Updated: Dec 21 '12 at 02:11 AM