关闭

发送JSON数据,返回JSON结果

237人阅读 评论(0) 收藏 举报
 
 public void Write()
        {
            string url = "http://127.0.0.1:80/api";
            string data ="q={\"token\":\"0539ba7d2bea47faa6f312b2ab491283\",\"data\":{\"trackingNumber\":\"\",\"statusChange\":\"A\",\"labelInfoArray\":[{\"value\":\"\",\"code\":\"AreaName\"},{\"value\":\"4\",\"code\":\"addCode\"}],\"ttPacketId\":\"000301P12555541\",\"logisticsSysId\":\"\"}}";
            Common(url,data);
        }


        private void Common(string url,string data)
        {
            //string apikey = "HY01";
            //string apiid = "HY01";
            StringBuilder sb = new StringBuilder();

            sb.Append(data);
            string Postdata = sb.ToString().Replace(" ", "");

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            byte[] arrB = encode.GetBytes(Postdata);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = arrB.Length;

            myRequest.CookieContainer = cook;
            Stream outStream = myRequest.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            string sst = "";
            HttpWebResponse HttpWResp;
            try
            {
                HttpWResp = (HttpWebResponse)myRequest.GetResponse();
                cookie = HttpWResp.Headers.Get("Set-Cookie").ToString();
            }
            catch (WebException ex)
            {
                HttpWResp = (HttpWebResponse)ex.Response;
            }
            //finally
            //{
            //    Response.Close();
            //}

            Stream myStream = HttpWResp.GetResponseStream();
            using (StreamReader sr = new StreamReader(myStream, Encoding.UTF8))
            {
                sst = sr.ReadToEnd();
            }
            str.InnerText = sst;
            //var jObjectnum = JObject.Parse(sst);
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:51851次
    • 积分:1081
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:37篇
    • 译文:0篇
    • 评论:3条