Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create web service to save data in database

C#
public string SaveData(String resultData) //save data to database
{
    DLR service = new DLR();
    Context.Response.ContentType = "application/json";
    string filePath = Server.MapPath("~/Error.txt");
    using (StreamWriter writer = new StreamWriter(filePath, true))
    {
        writer.WriteLine("Json is sent :" + resultData +
           "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
        writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
    }
    RootObject obj = JsonConvert.DeserializeObject<RootObject>(resultData);
        foreach (JsonData item in obj.results)
        {
            {
                var connStr = ConfigurationManager.ConnectionStrings["myCon1"].ConnectionString;
                SqlConnection con = new SqlConnection(connStr);
                try
                {
                    SqlCommand cmd = new SqlCommand("[insert_RDL]", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@msgId", item.msgId);
                    cmd.Parameters.AddWithValue("@to_mobile", item.to);
                    cmd.Parameters.AddWithValue("@status", item.status);
                    cmd.Parameters.AddWithValue("@ttest", "");
                    cmd.Parameters.AddWithValue("@newtest", "");
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                finally
                {
                    //                    
                }
            }
        }
        return "OK ";
    }


When i post the json by ajax in aspx page or by PHP SoapClient the page is sending json without problem .

But post it by PHP Curl
PHP
<?php
$data = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}' ;
$param = array('resultData' => $data);
var_dump($param);
echo "<hr>";
print_r($param);
$headers = array('Accept: application/json','Content-Type: application/json', );
echo "<hr>";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, "http://asdm.sa/DLR.asmx/SaveData");
curl_setopt($ch, CURLOPT_POSTFIELDS, $param); 

 $result =curl_exec($ch);

print_r($result);
echo "<hr>";
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}
?>


A problem a json is sent or sent NULL
{"Message":"Invalid JSON primitive: --------------------------60620f7a041fa3f6.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}


How i can sloved it

Thank you


What I have tried:

I used Postman to create the curl command for reference.
But Same problem >
Posted
Comments
Shashank Laxman 5-Feb-18 8:09am    
convert pure json into json string and try

{ \"test\":\"some data\" }
Member 12857222 5-Feb-18 10:56am    
where
in web service or PHP page
David_Wimbley 5-Feb-18 12:56pm    
Take a minute and think about the code you've written. Where are you consuming the code that requires you to manipulate the JSON. You are using PHP to access a ASP.NET web service (asmx) so where would it make sense that you need handle the conversion mentioned.

I'm trying to get you to think for yourself instead of attempting to just have answers provided for you, you'll never learn this way.
Member 12857222 5-Feb-18 15:19pm    
I read the code well
The variable (resultData) is issued from PHP by cURL is json string

also the variable recived in method (SaveData) is string
So no problem for conversion .
Thank You
Shashank Laxman 6-Feb-18 0:31am    
in PHP page,json should be in string format,try if it works after converting to JSON string


$data = '{\"results\": [{\"msgId\": \"001\",\"to\": \"9665312114\",\"status\": \"D\"}, {\"msgId\": \"859911880\",\"to\": \"966535112578\",\"status\": \"N\"}, {\"msgId\": \"859911880\",\"to\": \"966535112579\",\"status\": \"S\"}]}' ;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900