Esempio n. 1
0
        private string GetShortApiUrl(ShortApiUrlType type, out string strPost)
        {
            string result = "";

            switch (type)
            {
            case ShortApiUrlType.goo_gl:
                result  = "http://goo.gl/api/url";
                strPost = "&[email protected]&url={0}";
                break;

            case ShortApiUrlType.dwn_cn:
                result  = "http://dwz.cn/create.php";
                strPost = "&url={0}";
                break;

            case ShortApiUrlType.t_cn:
                //xml:http://api.t.sina.com.cn/short_url/shorten.xml
                //json:http://api.t.sina.com.cn/short_url/shorten.json
                //source:应用的appkey
                //url_long:需要转换的长链接
                result  = "http://api.t.sina.com.cn/short_url/shorten.json?source=1338661855";
                strPost = "&url_long={0}";
                break;

            default:
                strPost = null;
                break;
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取Url别名
        /// </summary>
        /// <param name="url">需要处理的Url</param>
        /// <param name="type"></param>
        /// <returns>返回Url别名</returns>
        public string GetApiShortUrl(string url, ShortApiUrlType type = ShortApiUrlType.dwn_cn)
        {
            try
            {
                string         strPost;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GetShortApiUrl(type, out strPost));
                string         post    = string.Format(strPost, System.Net.WebUtility.UrlEncode(url));
                request.ServicePoint.Expect100Continue = true;
                request.Method        = "post";
                request.UserAgent     = "toolbar";
                request.ContentLength = post.Length;
                request.ContentType   = "application/x-www-form-urlencoded";
                request.Headers.Add("Cache-Control", "no-cache");
                using (Stream requestStream = request.GetRequestStream())
                {
                    byte[] postBuffer = Encoding.ASCII.GetBytes(post);
                    requestStream.Write(postBuffer, 0, postBuffer.Length);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    string encoding = response.ContentEncoding;
                    if (encoding == null || encoding.Length < 1)
                    {
                        encoding = "UTF-8"; //默认编码
                    }
                    using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        url = responseReader.ReadToEnd();
                    }
                }

                return(url);
            }
            catch
            {
                return(url);
            }
        }