新浪短连接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 @   听海漫步  阅读(1322)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示