Get webapi asynchronous callback function parameters

Needless to say, asynchronous callbacks, I believe people who have used them can feel how disgusting it is. If asynchronous callbacks such as Alipay and WeChat are better, the writing is more popular, and there are examples to see. If you encounter a second cooperation Fang is finished, the API is written in a mess, and there is no case call yet. Recently, I encountered a company that wrote an API for me in java. The parameters of the callback function were passed in response.header and body. I Cao, and then told me that the parameters are In the request header and request body. Because there is nothing, I just said this sentence, and I was stunned at the time, because I had never heard of it. The request body in the net said so, but fortunately I can get the request headers they said in request.Headers. . , and then after various pondering, they really figured out their request body. It turns out that the request body they said is an http request, and if it is called by fidder, it appears in the body, so they call him the request body, just find the reason Ok, here is the code to get the request body:

  string key = Request.Headers["key"].ToString(); This is to get the request header

Let's start to get the request body, all request bodies are in our inputstream

 Stream reqStream = Request.InputStream;


byte[] buffer = new byte[(int)reqStream.Length];

 int s = reqStream.Read(buffer, 0, (int)reqStream.Length);

So far, we have actually obtained the request body. The following is the string in the parsing request body.

string req = System.Text.Encoding.Default.GetString(buffer);

  req = Server.UrlDecode(req);

At this point, req is the string of the request body we obtained. If it is json, convert it to json for parsing. The following is to parse the json format.

 JObject jo = (JObject)JsonConvert.DeserializeObject(req);

 string useruuid = jo["useruuid"].ToString();//用户id


After the final processing is completed, the string success table name is returned to the other party. We successfully received and processed the request.

 Response.Write("SUCCESS");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326028840&siteId=291194637