0

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.

2
  • I'm pretty sure wc has a UploadSomething (idr if its UploadValues) and you need to add multipart to it which is annoying and ridiculous. I don't remember how to do it offhand but i'm sure this will help someone answer Commented Nov 18, 2013 at 6:31
  • I m not sure about c# if you are passing parameters via POST..But your php code looks fine.. Commented Nov 18, 2013 at 6:32

2 Answers 2

1

Add

wc.Headers.Add("Content-Type","application/x-www-form-urlencoded");

before sending request, and also remove parentheses around Message from

wc.UploadValues(URI, Message);

call

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late reply, and thank you, though my issue continues. I'm almost completely sure it's something to do with my server now, but I don't know how to go about solving this.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.