.NET代码中实现Get请求和Post请求

GET请求:

 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
            {
                //构造一个Web请求的对象
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strURL);
                //获取web请求的响应的内容
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

                //通过响应流构造一个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("消息推送Get请求发生异常:" + ex.Message);
                jres.Add("code", new JValue("9999"));
                jres.Add("msg", new JValue(ex.Message));
            }

            return jres;
        }

 

POST请求:

  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("消息推送Post请求发生异常:" + ex.Message);
                jres.Add("code", new JValue("9999"));
                jres.Add("msg", new JValue(ex.Message));
            }
            return jres;

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值