Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
if (data["ReturnValue"].Equals("0"))
{
List<M_Ninushi> m_ninushis = new M_NINUSHI_DAO().GetList(data["LastUpdateDate"]);
string data_m_ninushi = JsonConvert.SerializeObject(m_ninushis);
string sentResponse = Util.FilterData(data_m_ninushi);
Dictionary<string, string> dataResponse = JsonConvert.DeserializeObject<Dictionary<string, string>>(sentResponse);
if (dataResponse["ReturnValue"].Equals("0"))
{
return 0;
}
else
{
return 1;
}
}
这个id我的代码在webservice中使用asp.net。我使用HttpWebRequest发送数据到symfony2 apiFilterData
XElement xdoc = XElement.Load(configFileName);
var objStringConnection = xdoc.Descendants("URL").Select(e => new { filter_data = e.Descendants("URL_FILTER_DATA").FirstOrDefault().Value }).SingleOrDefault();
string urlAddress = objStringConnection.filter_data;
System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
Dictionary<string, string> json = new Dictionary<string, string>();
json.Add("M_Ninushi", data);
byte[] dataSent = Encoding.ASCII.GetBytes(json.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//application/x-www-form-urlencoded
request.ContentLength = dataSent.Length;
Stream writer = request.GetRequestStream();
writer.Write(dataSent, 0, dataSent.Length);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string dataResponse = readStream.ReadToEnd();
response.Close();
readStream.Close();
return dataResponse;
}
这个id我的代码在webservice中使用asp.net。我使用HttpWebRequest发送数据到symfony2 api我知道如何发送数据,但我不知道如何在symfony2中获取数据。谁来帮帮我
c#代码更正
首先,我们需要更正发送到承载Symfony2应用程序的服务器的Content-Type
。您发送的数据不是application/x-www-form-urlencoded
的正确格式。修改为application/json
json_decode()
只支持UTF-8编码的字符串。因此,您必须使用Encoding.UTF8.GetBytes
而不是Encoding.ASCII.GetBytes
。
Dictionary.toString()
不返回JSON字符串。使用Json.NET
.
在Symfony2中接收JSON数据
在Controller
中,您可以使用SymfonyComponentHttpFoundationRequest::getContent()
来检索表单内容。
<?php
namespace CompanyCodeExampleBundleController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpKernelExceptionHttpException;
class RestAPIController extends Controller
{
public function doSomethingInterestingAction(Request $request)
{
if($request->headers->get('Content-Type') !== 'application/json') {
throw $this->createBadRequestException();
}
$jsonData = json_decode($request->getContent(), true);
if($jsonData === null) {
throw $this->createBadRequestException();
}
// DO SOMETHING WITH $jsonData
}
protected function createBadRequestException()
{
return new HttpException(400, 'This endpoint expects JSON data in the POST body and requires Content-Type to be set to application/json.');
}
}