Web Service receives JSON strings

In the evening, my colleague of the company, temporarily called A, A: "The A M interface of XXX textile in our company is not accessible, let me see." I said, "The interface is not written with an example of AJAX asynchronous request, refer to, I have tested the interface, all are accessible." A: "I'm not going AJAX. I'm going to the CS side." I said, "Oh, I see. The HttpWebRequest on the CS side simulates the AJAX request on the front end. I wrote it once before. I'll write a DEMO debug to see it."

The results are as follows.

Error example, because CS cannot prefix: jsonData

The correct AJAX example is preceded by jsonData (because the data in jsonData is retrieved in the background, which cannot be transferred on the CS side)

 

After searching for a half-day solution on the Internet, we can use the context to get the JSON data stream and then restore the stream back to the JSON string.

1             Stream s = HttpContext.Current.Request.InputStream;//Get json Character stream
2             //Restore data stream
3             byte[] b = new byte[s.Length];
4             s.Read(b, 0, (int)s.Length);
5             string jsontext = Encoding.UTF8.GetString(b); //JSON Character string

 

cs-side Http WebRequest example

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             string res = PostWebRequest("http://XXXXXXX/webService/eCimsService.asmx/GetHullParts", "{\"ProjNo\":\"H001\",\"SectionNo\":\"C002\",Data:[]}", Encoding.UTF8);
 4         }
 5         /// <summary>
 6         /// Post data interface
 7         /// </summary>
 8         /// <param name="postUrl">Interface address</param>
 9         /// <param name="paramData">Submission json data</param>
10         /// <param name="dataEncode">Coding mode(Encoding.UTF8)</param>
11         /// <returns></returns>
12         private static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
13         {
14             string responseContent = string.Empty;
15             try
16             {
17                 byte[] byteArray = dataEncode.GetBytes(paramData); //conversion
18                 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
19                 webReq.Method = "POST";
20                 webReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
21                 webReq.Accept = "*/*"; //Note: In the process of debugging, report 415, which may need to be modified.
22                 webReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
23                 webReq.ContentLength = byteArray.Length;
24                 using (Stream reqStream = webReq.GetRequestStream())
25                 {
26                     reqStream.Write(byteArray, 0, byteArray.Length);//Write parameters
27                     //reqStream.Close();
28                 }
29                 using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
30                 {
31                     using (Stream myResponseStream = response.GetResponseStream())
32                     {
33                         using (StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8))
34                         {
35                             responseContent = myStreamReader.ReadToEnd().ToString();
36                         }
37                     }
38                 }
39             }
40             catch (Exception ex)
41             {
42                 return ex.Message;
43             }
44             return responseContent;
45         }

Tags: PHP JSON encoding Windows

Posted on Mon, 07 Oct 2019 22:08:26 -0700 by jasonbullard