c#在cs中 带参的POST请求,传递文本数据

  using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 

/// <summary>
        /// 带参的POST请求,传递文本数据
        /// </summary>
        /// <param name="url">例如 192.168.1.222:8080/getMaterialsBySpacePlanIdToClient</param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public string HttpPostRequest(string url, string paramData)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf8"; // 必须指明字符集!
            httpWebRequest.Timeout = 20000;

            // 参数
         
              
                // 给文本数据编码
            byte[] data = Encoding.UTF8.GetBytes(paramData); // 必须与ContentType中指定的字符集一致!

                // 往请求的流里写数据
                using (Stream stream = httpWebRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
           

            // 从响应对象中获取数据
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
            string responseContent = streamReader.ReadToEnd();

            streamReader.Close();
            httpWebResponse.Close();
            httpWebRequest.Abort();

            return responseContent;
        }

 

 

调用方式

    string postUrl = "http://open.wangpos.com/wopengateway/api/entry";
                  string paramdata = @"access_token=" + accessToken + "&nonce=" + _nonce + "&service="
                      + WangPosMsgService + "&signature=" + _signature + "&timestamp=" +
                      _timestamp + "&device_en=" + device_en + "&biz_code=" + WangPosBiz_code
                      + "&data=" + strTuisong.ToString();

                  TKF.Common.showinfo.ShowInfo("推送的字符串===" + paramdata, true);

                  string MsgJson = HttpPostRequest(postUrl, paramdata);
                  TKF.Common.showinfo.ShowInfo("推送返回值MsgJson===" + MsgJson, true);


                  JObject joMsg = JObject.Parse(MsgJson);
                  string MsgStatus = joMsg["status"].ToString();
                  string Msg = joMsg["info"].ToString();
                  TKF.Common.showinfo.ShowInfo("MsgStatus===" + MsgStatus, true);
                  if (MsgStatus.Trim() == "0")
                  {
                      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "tkf.Alert('已经完成,请付款')", true);
                  }
                  else
                  {
                      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "tkf.Alert('" + Msg + "')", true);
                  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值