百度鹰眼接口访问c#版

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


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


    /// <summary>
    /// 发送HttpPost请求
    /// </summary>
    /// <param name="url">请求的URL</param>
    /// <param name="param">请求的参数。(参数1=值1&参数2=值2&参数3=值3。。。。。。。)</param>
    /// <returns>请求结果(json对象)</returns>
    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<object>(strValue);
            }
        }


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

    /// <summary>
    /// 发送HttpGet请求
    /// </summary>
    /// <param name="url">请求的URL</param>
    /// <param name="param">请求的参数</param>
    /// <param name="apikey">开发者apikey</param>
    /// <returns>请求结果(json对象)</returns>
    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<object>(strValue);
        }
        //示例调用
        //string url = "http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid";
        //string param = "参数1=值1&参数2=值2&参数3=值3。。。。。。。";
        //string result = Get(url, param);
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容