百度鹰眼接口访问c#版

using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;


/// 
/// 百度鹰眼调用帮助类
/// 
public class BDYingYanAPIHelper
{


    /// 
    /// 发送HttpPost请求
    /// 
    /// 请求的URL
    /// 请求的参数。(参数1=值1&参数2=值2&参数3=值3。。。。。。。)
    /// 请求结果(json对象)
    public static object Post(string url, string param)
    {
        string strURL = url;
        HttpWebRequest request;
        request = (HttpWebRequest)WebRequest.Create(strURL);
        request.Method = "POST";

        request.ContentType = "application/x-www-form-urlencoded";
        string paraUrlCoded = param;
        byte[] payload;
        payload = Encoding.UTF8.GetBytes(paraUrlCoded);
        request.ContentLength = payload.Length;
        using (Stream writer = request.GetRequestStream())
        {
            writer.Write(payload, 0, payload.Length);
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();
            Stream s;
            s = response.GetResponseStream();
            string StrDate = "";
            string strValue = "";
            using (StreamReader Reader = new StreamReader(s, Encoding.UTF8))
            {
                while ((StrDate = Reader.ReadLine()) != null)
                {
                    strValue += StrDate + "\r\n";
                }
                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Deserialize(strValue);
            }
        }


        //示例调用
        //string url = "http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid";
        //string param = "参数1=值1&参数2=值2&参数3=值3。。。。。。。";
        //string result = Post(url, param);
    }

    /// 
    /// 发送HttpGet请求
    /// 
    /// 请求的URL
    /// 请求的参数
    /// 开发者apikey
    /// 请求结果(json对象)
    public static object Get(string url, string param)
    {
        string strURL = url + '?' + param;
        HttpWebRequest request;
        request = (HttpWebRequest)WebRequest.Create(strURL);
        request.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "GET";
        // 添加header
        HttpWebResponse response;
        response = (HttpWebResponse)request.GetResponse();
        Stream s;
        s = response.GetResponseStream();
        string StrDate = "";
        string strValue = "";
        using (StreamReader Reader = new StreamReader(s, Encoding.UTF8))
        {
            while ((StrDate = Reader.ReadLine()) != null)
            {
                strValue += StrDate + "\r\n";
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize(strValue);
        }
        //示例调用
        //string url = "http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid";
        //string param = "参数1=值1&参数2=值2&参数3=值3。。。。。。。";
        //string result = Get(url, param);
    }

}







你可能感兴趣的:(百度鹰眼接口访问c#版)