发送请求并返回

        public static string GetShortUrl(string url)
        {
            string paramstr = "{\"url\":\"" + url + "\"}";
            string api = "https://dwz.cn/admin/v2/create";
            Stream responseStream = null;
            StreamReader sr = null;
            Stream requestStream = null;
            string result = null;
            byte[] data = Encoding.UTF8.GetBytes(paramstr);
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(api);
                httpRequest.ContentType = "application/json";
                httpRequest.Method = "POST";
                httpRequest.Headers.Add("Token", "a82e54954acb70c58a922ce9b2fe6fbd");
                //发送数据
                httpRequest.ContentLength = data.Length;
                requestStream = httpRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
                //获取响应内容
                HttpWebResponse HttpWResp = (HttpWebResponse)httpRequest.GetResponse();
                responseStream = HttpWResp.GetResponseStream();
                sr = new StreamReader(responseStream, Encoding.UTF8);
                result = sr.ReadToEnd();
            }
            catch (Exception e)
            {

            }
            finally
            {
                if (sr != null)
                    sr.Close();
                if (responseStream != null)
                    responseStream.Close();
                if (requestStream != null)
                    requestStream.Close();
            }
            if (!string.IsNullOrEmpty(result))
            {
                LitJson.JsonData json = LitJson.JsonMapper.ToObject(result);
                return json["ShortUrl"].ToString();
            }
            return url;
        }

  百度生成短链接  https://dwz.cn/console/apidoc?tdsourcetag=s_pctim_aiomsg

posted @ 2019-02-15 16:32  wonderfulviews  阅读(187)  评论(0编辑  收藏  举报