Skip to content
Advertisement

c# posting to PHP working on localhost, but not on production server

I need to post a username and a password, to a server to validate it, then return a true or false. This is fully working on my local machine, but when I try it on the production server, it is not receiveing the POST values (it is returning the “noPOST” back to the c# code).

C# code:

            string username = "user@xyz.com";
            string password = "pass";
            string urlAddress = "https://server/validate.php";

            using (WebClient client = new WebClient())
            {
                NameValueCollection postData = new NameValueCollection();

                postData.Add("username",username);
                postData.Add("password",password);
                    
                string ret = Encoding.UTF8.GetString(client.UploadValues(urlAddress, postData));
                Console.WriteLine(ret);
            } 

PHP Code:

if (isset($_POST['username']) && isset($_POST['password'])) {
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    $check = "CALL validate('$username');";
    $res = mysqli_query($conn,$check);
    $row = mysqli_fetch_assoc($res);
    $pass = $row['password'];
    if (password_verify($password, $pass)) {
        echo("true");
    }else{
        echo("false");
    }
}else{
    echo("noPOST");
} 

[EDITED]

C#:

            var client = new HttpClient();
            client.BaseAddress = new Uri("https://productionserverdomain.xy");
            var request = new HttpRequestMessage(HttpMethod.Post, "/sync.php");

            var requestContent = string.Format("site={0}&username={1}&password={2}", Uri.EscapeDataString("https://productionserverdomain.xy"),
                Uri.EscapeDataString(username), Uri.EscapeDataString(password));
            request.Content = new StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded");

            var response = await client.SendAsync(request);

            var res = response.Content.ReadAsStringAsync();

PHP Code:

if (isset($_POST['username']) && isset($_POST['password'])) {
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    $check = "CALL validate('$username');";
    $res = mysqli_query($conn,$check);
    $row = mysqli_fetch_assoc($res)
    $pass = $row['password'];

    if (password_verify($password, $pass)) {
        http_response_code(200);
    }else{
        http_response_code(401);
    }
}else{
    echo("noPOST");
}

Advertisement

Answer

You may have two issues:

1- Something related to proxy or reverse proxy, redirector in front of your PHP service in your production environment.

2- Type of sending the request, Solution: Use HttpClient and check these steps for more details.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement