[英]how to pass values to request object from php file
I am having a small problem. 我有一个小问题。 I have developed ac# file which accepts data in Request object.
我已经开发了一个ac#文件,该文件在Request对象中接受数据。 the Request object contains a string and then i performs various operations on that string but the problem is that I need to call this aspx file from a PHP file.
Request对象包含一个字符串,然后我对该字符串执行各种操作,但是问题是我需要从PHP文件调用此aspx文件。
This is how i am accepting data in the ASPX file 这就是我接受ASPX文件中的数据的方式
protected void Page_Load(object sender, EventArgs e)
{
try{
byte[] byteArray;
byteArray = Request.BinaryRead(Request.TotalBytes);
// This bytes array to string method perfectly converts the request data into string.
string xmlString = Utility.Utility.ByteArrayToString(byteArray);
now if i am passing a JSON string from PHP file then please guide me how can i do that 现在,如果我要从PHP文件传递JSON字符串,请指导我如何做
If it is a JSON string then you could use a JSON serializer in order to parse deserialize this string into a class. 如果它是JSON字符串,则可以使用JSON序列化程序将此字符串反序列化为一个类。 For example you could use the built-in
JavaScriptserializer
class or Json.NET
. 例如,您可以使用内置的
JavaScriptserializer
类或Json.NET
。
Let's suppose that the PHP file sent the following JSON to the body of a POST request: 假设PHP文件将以下JSON发送到POST请求的正文中:
{"foo":"bar","baz":123}
Now you could define a corresponding model that will match this JSON structure: 现在,您可以定义一个匹配此JSON结构的相应模型:
public class MyModel
{
public string Foo { get; set; }
public int Baz { get; set; }
}
and then read the JSON payload from the request and use the serializer to deserialize it back to your model: 然后从请求中读取JSON有效负载,并使用序列化器将其反序列化回模型:
protected void Page_Load(object sender, EventArgs e)
{
using (var reader = new StreamReader(Request.InputStream))
{
string json = reader.ReadToEnd();
var serializer = new JavaScriptSerializer();
MyModel model = serializer.Deserialize<MyModel>(json);
// you could use the model here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.