美文网首页
百度APIStore接口访问c#版

百度APIStore接口访问c#版

作者: p了个f | 来源:发表于2016-12-19 11:01 被阅读75次
#region << 版 本 注 释 >>
/*
 * ========================================================================
 * CLR版本 :4.0.30319.42000
 * 类 名 称:BDAPIStoreHelper
 * 机器名称:ENGOO-YPF
 * 命名空间:YPF.Common.Web处理
 * 文 件 名:BDAPIStoreHelper
 * 创建时间:2016/12/14 17:55:21
 * 作    者:杨朋飞
 * 说   明:
 * 修改时间:
 * 修 改 人:
 * ========================================================================
*/
#endregion

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

namespace YPF.Common.Web处理
{
    /// <summary>
    /// 百度APIStore调用帮助类
    /// </summary>
    public class BDAPIStoreHelper
    {


        /// <summary>
        /// 发送HttpPost请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="param">请求的参数</param>
        /// <param name="apikey">您自己的apikey</param>
        /// <returns>请求结果(json对象)</returns>
        public static object Post(string url, string param, string apikey)
        {
            string strURL = url;
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            // 添加header
            request.Headers.Add("apikey", apikey);
            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 = request(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 apikey)
        {
            string strURL = url + '?' + param;
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "GET";
            // 添加header
            request.Headers.Add("apikey", apikey);
            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);
            }
        }

    }
}

相关文章

网友评论

      本文标题:百度APIStore接口访问c#版

      本文链接:https://www.haomeiwen.com/subject/hwftvttx.html