新浪短连接API免登陆免认证实例

string source = "source=2849184197"; // APP Key,这个可以根据自己需要去网上搜索
string url_long = $"url_long=http://baidu.com?id={id}"; //自己的访问地址
string data = Func.HttpNet.GetData($"?{ source}&{url_long}", $"http://api.weibo.com/2/short_url/shorten.json");//get请求API返回短连接数据
var jsonObject = LitJson.JsonMapper.ToObject<Func.WeiBoLnk>(data);//返回的string格式转换成对象

实体类对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Game.Web.Func
{
    public class WeiBoLnk
    {
        public List<Urls> urls { get; set; }

    }

    public class Urls
    {
        public bool result { get; set; }
        public string url_short { get; set; }
        public string url_long { get; set; }
        public string object_type { get; set; }
        public int type { get; set; }
        public string object_id { get; set; }
    }
}

API请求的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Game.Web.Func
{
    public class HttpNet
    {
        /// <summary>
        /// 以GET 形式获取数据
        /// </summary>
        /// <param name="RequestPara"></param>
        /// <param name="Url"></param>
        /// <returns></returns>

        public static string GetData(string RequestPara, string Url)
        {
            RequestPara = RequestPara.IndexOf('?') > -1 ? (RequestPara) : ("?" + RequestPara);

            WebRequest hr = HttpWebRequest.Create(Url + RequestPara);

            byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
            hr.Method = "GET";

            System.Net.WebResponse response = hr.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string ReturnVal = reader.ReadToEnd();
            reader.Close();
            response.Close();

            return ReturnVal;
        }
  } }

 

转载请标明出处!谢谢!
posted @ 2019-04-27 16:33  听海漫步  阅读(1318)  评论(0编辑  收藏  举报