c# Post JSON

Permudious

New Member
Dec 28, 2014
15
0
Hi there,

I have written a code which seems like working but in c# I can't post a JSON request. When I do the same post in Postman, it is working fine.
What do I wrong?

C#:
            string username = "demo";
            string password = "password";
            string firstname = "John";
            string lastname = "Smith";
            string url = "http://localhost/index.php";

            //setup some variables end

            string result = "";
            String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
            StreamWriter myWriter = null;

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";
            objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myWriter.Close();
            }
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Hi there,

I have written a code which seems like working but in c# I can't post a JSON request. When I do the same post in Postman, it is working fine.
What do I wrong?

C#:
            string username = "demo";
            string password = "password";
            string firstname = "John";
            string lastname = "Smith";
            string url = "http://localhost/index.php";

            //setup some variables end

            string result = "";
            String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
            StreamWriter myWriter = null;

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.Method = "POST";
            objRequest.ContentLength = strPost.Length;
            objRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(strPost);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myWriter.Close();
            }
Code:
string url = "http://localhost/index.php";

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.ContentType = "application/json";
objRequest.Method = "POST";

try

{

using (StreamWriter streamWriter = new StreamWriter(objRequest.GetRequestStream())){
    string json = "{\"username\":\"Demo\"," +
                  "\"password\":\"pass\"," +
                  "\"firstname\":\"John\"," +
                  "\"lastname\":\"Smith\"}";
    streamWriter.Write(json);
}

HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    //Do what you want with the result here
}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}
 

Permudious

New Member
Dec 28, 2014
15
0
Code:
string url = "http://localhost/index.php";

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.ContentType = "application/json";
objRequest.Method = "POST";

try

{

using (StreamWriter streamWriter = new StreamWriter(objRequest.GetRequestStream())){
    string json = "{\"username\":\"Demo\"," +
                  "\"password\":\"pass\"," +
                  "\"firstname\":\"John\"," +
                  "\"lastname\":\"Smith\"}";
    streamWriter.Write(json);
}

HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    //Do what you want with the result here
}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

Hi Jay, I have copied an paste the code, there was an error but I have fixed that. But it is not working. He is not finishing his post. :-(
 

Permudious

New Member
Dec 28, 2014
15
0
What is it doing? Are you sure it's not on the server that's the problem? Have you stepped through the c# using a debugger?
It should post information into PHP.

Here below the code that I have used. In line 24 there was an error. In your code it was 'httpWebRequest' but this is not in use, so I have changed that to 'ObjRequest'
C#:
string url = "http://localhost/index.php";

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.ContentType = "application/json";
            objRequest.Method = "POST";

            try

            {

                using (StreamWriter streamWriter = new StreamWriter(objRequest.GetRequestStream()))
                {
                    string json =   "{\"Key\":\"1234-TEST-5678-JSON\"," +
                                    "\"MachineID\":\"THIS-MACHINE\"," +
                                    "\"Company\":\"UNKNWN\"," +
                                    "\"Project\":\"JSON Test\"," +
                                    "\"Contact\":\"Ikke\"," +
                                    "\"Mail\":\"[email protected]\"," +
                                    "\"ActivationDate\":\"2020-07-26 00:00:00\"," +
                                    "\"token\":\"a46c10a4d4d28a6ef1121a393e971299\"}";
                    streamWriter.Write(json);
                }

                HttpWebResponse httpResponse = (HttpWebResponse)objRequest.GetResponse();
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    //Do what you want with the result here
                }
            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
It should post information into PHP.

Here below the code that I have used. In line 24 there was an error. In your code it was 'httpWebRequest' but this is not in use, so I have changed that to 'ObjRequest'
C#:
string url = "http://localhost/index.php";

            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
            objRequest.ContentType = "application/json";
            objRequest.Method = "POST";

            try

            {

                using (StreamWriter streamWriter = new StreamWriter(objRequest.GetRequestStream()))
                {
                    string json =   "{\"Key\":\"1234-TEST-5678-JSON\"," +
                                    "\"MachineID\":\"THIS-MACHINE\"," +
                                    "\"Company\":\"UNKNWN\"," +
                                    "\"Project\":\"JSON Test\"," +
                                    "\"Contact\":\"Ikke\"," +
                                    "\"Mail\":\"[email protected]\"," +
                                    "\"ActivationDate\":\"2020-07-26 00:00:00\"," +
                                    "\"token\":\"a46c10a4d4d28a6ef1121a393e971299\"}";
                    streamWriter.Write(json);
                }

                HttpWebResponse httpResponse = (HttpWebResponse)objRequest.GetResponse();
                using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    //Do what you want with the result here
                }
            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }
Okay in your first example you were attempting to register a user, now you're posting information on machine and company - Is your index matching this post request?!?
 

Permudious

New Member
Dec 28, 2014
15
0
Okay in your first example you were attempting to register a user, now you're posting information on machine and company - Is your index matching this post request?!?

Correct, I was not the intention to show the real post. When I post the exact keys and values via Postman it is working fine.
 

Skythrust

Member
Jul 9, 2019
133
7
You can give this a try, not tested but it should work.

C#:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/index.php");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    Key = "1234-TEST-5678-JSON",
                    MachineID = "THIS-MACHINE",
                    Company = "UNKNWN",
                    Project = "JSON Test",
                    Contact = "Ikke",
                    Mail = "[email protected]",
                    ActivationDate = "2020-07-26 00:00:00",
                    token = "a46c10a4d4d28a6ef1121a393e971299"
                });
                streamWriter.Write(json);
                MessageBox.Show(streamWriter.ToString());
                streamWriter.Flush();
                streamWriter.Close();

            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
 

Permudious

New Member
Dec 28, 2014
15
0
Sorry not working..

this is by the way the other side of the form (php page)

PHP:
$key = $_GET['Key'];
$machineid = $_GET['MachineID'];
$company = $_GET['Company'];
$project = $_GET['Project'];
$contact = $_GET['Contact'];
$mail = $_GET['Mail'];
$actdate = $_GET['ActivationDate'];
$token = $_GET['token'];

$conn = new mysqli($MySQLi_DataSource, $MySQLi_Username, $MySQLi_Password, $MySQLi_InitialCatalog);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$query =     "INSERT INTO ApplicationLicenses (LicenseKey, LicenseType, MachineID, CompanyName, Project, ContactPerson, Email, Activation)
VALUES ('". $key."','Full','". $machineid."','". $company."','". $project."','". $contact."','". $mail."','". $actdate."')";

if ($conn->query($query) === TRUE) { echo("Error description: " . $mysqli -> error); } else {  }
$conn->close();
 

Users who are viewing this thread

Top