当前位置:网站首页>. Net code to implement get request and post request

. Net code to implement get request and post request

2022-06-13 07:03:00 AndroidOrCSharp

GET request :

 private JObject doGet(Dictionary<String, String> param)
        {
            JObject jres = new JObject();

            object obj = new { notifyType = 1, taCode = strTANO, tradeDt = DateTime.Now.ToString("yyyyMMdd") };
            strData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

            string strURL = "https//192.168.1.1/channel/push";
            if (!strURL.EndsWith("?"))
            {
                strURL = strURL + "?";
            }
            strURL = strURL + strData;

            try
            {
                // Construct a Web The object of the request 
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL);
                // obtain web Content of the requested response 
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                // Construct a StreamReader
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string response = reader.ReadToEnd();
                reader.Close();
                myResponse.Close();

                jres.Add("code", new JValue("0"));
                jres.Add("msg", new JValue(response));
            }
            catch (Exception ex)
            {
                log.Info(" Message push Get Request exception :" + ex.Message);
                jres.Add("code", new JValue("9999"));
                jres.Add("msg", new JValue(ex.Message));
            }

            return jres;
        }

 

POST request :

  private JObject doPost(Dictionary<String, String> param)
        {
            object obj = new { notifyType = 1, taCode = strTANO, tradeDt = DateTime.Now.ToString("yyyyMMdd") };
            strData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

            string strURL = "https://192.168.1.1/post";
            JObject jres = new JObject();

            try
            {
                byte[] sendData = Encoding.UTF8.GetBytes(strData);
                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/json");
                byte[] retInfo = webClient.UploadData(strURL, "POST", sendData);
                string strInfo = System.Text.Encoding.UTF8.GetString(retInfo);
                JObject jre = Newtonsoft.Json.JsonConvert.DeserializeObject(strInfo) as Newtonsoft.Json.Linq.JObject;

                string requestState = jre["code"] is null ? jre["result"] is null ? "9999" : jre["result"].ToString() : jre["code"].ToString();
                if ("000000".Equals(requestState) || "success".Equals(requestState))
                {
                    jres.Add("code", new JValue("0"));
                }
                else
                {
                    jres.Add("code", new JValue("9999"));
                }
            }
            catch (Exception ex)
            {
                log.Info(" Message push Post Request exception :" + ex.Message);
                jres.Add("code", new JValue("9999"));
                jres.Add("msg", new JValue(ex.Message));
            }
            return jres;

        }

 

原网站

版权声明
本文为[AndroidOrCSharp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550514270.html