Read json in web service asmx

时间:2018-02-03 11:22:43

标签: c# php web-services

I create web service in c# to receive json and insert it to data base :

public string SaveData(string resultData)
{

DLR service = new DLR();

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,settings);
    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 the page is sending json without problem . and i see json in error.txt .

when i post the json by php :

<?php
$data = '{"results": [{"msgId": "4000","to": "9665312114","status": "D"}, 
{"msgId": "859911880","to": "966535112578","status": "N"}, 
{"msgId":"859911880","to": "966535112579","status": "S"}]}' ;
$phpObj =  json_decode($data);
print_r($phpObj);
$resultData =  json_encode($phpObj);
print_r($resultData);
$client = new SoapClient("http://asdm.sa/DLR.asmx?wsdl");
$response = $client->SaveData($data);
print_r($response);
?>

----------

The json is sending is NULL. why the page not sending the json.

when i open the error.txt :

Json is sent :{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]} Date :2/3/2018 6:59:02 PM


Json is sent :

Date :2/3/2018 7:31:35 PM

1 个答案:

答案 0 :(得分:0)

看起来问题在于发送参数。请尝试下一个代码

cluster_size