Search code examples
c#phppostdata

C# -> Php | Post Data


I've been trying to work with php post data for the past two days, and after being absolutely positive my code was logical, I gave up attempting to get it working myself.

Here's my C# code,

class SecureWeb
{
    /*
     *  CONSTRUCTOR
    */
    public SecureWeb()
    {
        //INITIALIZE

    }

    public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }
    public string Post(string URI, NameValueCollection Message)
    {
        try
        {
            string result = null;
            using (WebClient wc = new WebClient())
            {
                wc.Proxy = null;
                wc.Credentials = CredentialCache.DefaultCredentials;                    
                ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;
                byte[] bByte = wc.UploadValues(URI, (Message));
                result = Encoding.UTF8.GetString(bByte);
            }
            return result;
        }
        catch (Exception) { }
        return null;
    }
}

and here's my php code (yes I have tried just using a simple echo),

<?php
function getParams() {

    if (!isset($_POST['db'])) {
        return false;
    }
    if (!isset($_POST['user'])) {
        return false;
    }

    return true;
}

if (!getParams()) {

    echo('NULL_DATA');
    exit();
}
else {

    $user = $_POST['user'];
    echo ($user);
    exit();
}

?>

I'm receiving a 406 error claiming my request is 'Unacceptable'. I'm also fairly positive the issue at hand is my server, I'm just not sure what needs fixing or tweaking.


Solution

  • I played around with every possible header until I found a solution, seeing as to my web server was 100% convinced the issue wasn't on their end. Turns out, I NEEDED a User-Agent, because for some odd reason the request didn't acquire one by default.