2

After looking a bit around I came to this page. Here, I found some code to send a C# string to a PHP page.

However, after implementing it in my own program it did not work. Here is my code:

        private void executesend()
        {  
            using (WebClient client = new WebClient())
            {
                client.UploadString(url,"POST",keys);
            }
        }

For the PHP part, I have:

    <?php 
    mysql_connect("localhost", "", "") or die(mysql_error()); // Connect to database server(localhost) with username and password.
            mysql_select_db("dimittv89_dayz") or die(mysql_error()); // Select registration database.
            $name = $_GET["message"];
 if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $name = file_get_contents('php://input');

    $opdracht = "INSERT INTO 'keys', (`key`) VALUES ('$name')";
    print $name;
}

if (mysql_query($opdracht)){ 
echo "succesfully registerd to the melloniax u wil now be returned to our main page";
 }
 else{
 echo "hey something went wrong there ! please try again in a minute";
 }


 ?>

In the same topic one of the users also said to try this:

php?fmessage=testtesttest"

and write down the output using

$name = $_GET["message"];
print $name;

This did not work either. Am I doing something wrong?

Thanks for the help already

Well sofar i found out its not the send value thats wrong but the get value :

Username = Registry.CurrentUser.OpenSubKey("Username", true);
Name = "" + Username.GetValue("Uid");

in the regedit menu it says that the value is a REG_BINARY, are these readable with getvalue ?

16
  • 2
    Can you elaborate a bit on how it doesn't work? What happens exactly? Commented Feb 1, 2013 at 15:03
  • you probably didn't send to the correct php file, make sure you are writting the whole path for the php file, because the user that reply wanted to check if you're addressing to the correct page. Commented Feb 1, 2013 at 15:06
  • @MrLister i try to send a string i obtain in C# ( visual studio 10) to string url = "melloniax.com/receive.php?message=testtesttest"; the message is for the test message i tryd and i want the php page to get that string and send it to a php table through a querry Commented Feb 1, 2013 at 15:09
  • Can you included more of your code? It seems to me that you may have flow control problems in code. Commented Feb 1, 2013 at 15:15
  • 1
    I am guessing that the SQL statement is incorrect. Can you add to the answer the returned string from the client.UploadString(url,"POST",keys); You should be able to debug and just catch the returned value of the function. Commented Feb 1, 2013 at 15:54

1 Answer 1

1

use this code for c# and php :

private void executesend()
    {  

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                req.Method = "POST";
                string Data = "message="+keys;
                byte[] postBytes = Encoding.ASCII.GetBytes(Data);
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = postBytes.Length;
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                Stream resStream = response.GetResponseStream();

                var sr = new StreamReader(response.GetResponseStream());
                string responseText = sr.ReadToEnd();


            }
            catch (WebException)
            {

                MessageBox.Show("Please Check Your Internet Connection");
            }

}

and php

<?php 
    mysql_connect("localhost", "", "") or die(mysql_error()); // Connect to database server(localhost) with username and password.
            mysql_select_db("dimittv89_dayz") or die(mysql_error()); // Select registration database.

 if (isset($_POST['message']))
{
    $name = $_POST['message'];

    $opdracht = "INSERT INTO keys (key) VALUES ('$name')";
    print $name;
    if (mysql_query($opdracht)){ 
     echo "succesfully registerd to the melloniax u wil now be returned to our main page";
    }
    else{
     echo "hey something went wrong there ! please try`enter code here` again in a minute";
    }

}

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

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.