新浪短連接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;
        }
  } }

 

轉載請標明出處!謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM