C#长链接转短链接(调用新浪api)

来源:互联网 发布:菜鸟商城源码 编辑:程序博客网 时间:2024/05/10 06:51


 /// <summary>
 /// 长链接转短链接
 /// </summary>
 /// <param name="longUrl">长链接</param>
 /// <returns>短链接</returns>

public string LongToShortUrl(string longUrl)
        {
            string url = "
http://api.t.sina.com.cn/short_url/shorten.xml";
            string datas = string.Format("source={0}&url_long={1}", "209678993", longUrl);
            byte[] byteArray = Encoding.UTF8.GetBytes(datas);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();

            //接收返回信息
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = reader.ReadToEnd();
            XmlDocument xml=new XmlDocument();
            xml.LoadXml(result);
            return xml.SelectSingleNode("//url_short").InnerText;
           
        }

http://open.weibo.com/wiki/Short_url/shorten#JSON.E7.A4.BA.E4.BE.8B //新浪改短api说明


0 0