关于C#调用百度实时新闻API接口

时间:2024-04-06 17:33:32

百度实时新闻API接口地址:http://apistore.baidu.com/apiworks/servicedetail/1570.html

以下为C#的示例代码,如果没有apikey可注册百度账号获取

string url = "http://apis.baidu.com/songshuxiansheng/news/news";
string param = "";
string result = request(url,param);//返回的数据为json

/// <summary>
/// 发送HTTP请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="param">请求的参数</param>
/// <returns>请求结果</returns>
public static string request(string url, string param)
{
    string strURL = url + '?' + param;
    System.Net.HttpWebRequest request;
    request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
    request.Method = "GET";
    // 添加header
    request.Headers.Add("apikey", "您自己的apikey");
    System.Net.HttpWebResponse response;
    response = (System.Net.HttpWebResponse)request.GetResponse();
    System.IO.Stream s;
    s = response.GetResponseStream();
    string StrDate = "";
    string strValue = "";
    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
    while ((StrDate = Reader.ReadLine()) != null)
    {
        strValue += StrDate + "\r\n";
    }
    return strValue;
}

使用上面的方法

 static void Main(string[] args)
        {
            string url = "http://apis.baidu.com/songshuxiansheng/news/news";
            string param = "";
            string result = request(url, param);
            Console.WriteLine(result );
        } 

得到结果为

  关于C#调用百度实时新闻API接口

以上输出的内容不是我们想要的新闻,我们要把Unicode编码变成中文用下面的方法

public static string UnicodeToGB(string text)
        {
            System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})");
            if (mc != null && mc.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match m2 in mc)
                {
                    string v = m2.Value;
                    string word = v.Substring(2);
                    byte[] codes = new byte[2];
                    int code = Convert.ToInt32(word.Substring(0, 2), 16);
                    int code2 = Convert.ToInt32(word.Substring(2), 16);
                    codes[0] = (byte)code2;
                    codes[1] = (byte)code;
                    text = text.Replace(v, Encoding.Unicode.GetString(codes));
                }
            }
            return text;
        }

然后在次运行

 static void Main(string[] args)
        {
            string url = "http://apis.baidu.com/songshuxiansheng/news/news";
            string param = "";
            string result = request(url, param);
            string str = UnicodeToGB(result);
            string the_Json = str.Replace("\\", "");
            Console.WriteLine(the_Json);
         }

得到

关于C#调用百度实时新闻API接口

完整代码

using System;
using System.Text;
using System.IO;
using System.Net;
namespace Test
{
    class Program
    {
        
        static void Main(string[] args)
        {
            string url = "http://apis.baidu.com/songshuxiansheng/news/news";
            string param = "";
            string result = request(url, param);
            string str = UnicodeToGB(result);
            string the_Json = str.Replace("\\", "");
            Console.WriteLine(the_Json);
           
        }
        /// <summary>
        /// 发送HTTP请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="param">请求的参数</param>
        /// <returns>请求结果</returns>
        public static string request(string url, string param)
        {
            string strURL = url + '?' + param;
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "GET";
            // 添加header
            request.Headers.Add("apikey", "您自己的apikey");
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s;
            s = response.GetResponseStream();
            string StrDate = "";
            string strValue = "";
            StreamReader Reader = new StreamReader(s, Encoding.UTF8);
            while ((StrDate = Reader.ReadLine()) != null)
            {
                strValue += StrDate + "\r\n";
            }
            return strValue;
        }
        public static string UnicodeToGB(string text)
        {
            System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})");
            if (mc != null && mc.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match m2 in mc)
                {
                    string v = m2.Value;
                    string word = v.Substring(2);
                    byte[] codes = new byte[2];
                    int code = Convert.ToInt32(word.Substring(0, 2), 16);
                    int code2 = Convert.ToInt32(word.Substring(2), 16);
                    codes[0] = (byte)code2;
                    codes[1] = (byte)code;
                    text = text.Replace(v, Encoding.Unicode.GetString(codes));
                }
            }
            else
            { }
            return text;
        }
    }
}