首页 新闻 会员 周边

百度统计的api 获取实时访客的net 调用demo

0
悬赏园豆:50 [已解决问题] 解决于 2018-09-19 09:41

有没有人做过调用百度统计的数据,net 操作源码
是获取实时访客的数据,有搜索词,ip 等信息,通过接口获取到本地?
https://tongji.baidu.com/web/demo/trend/latest?siteId=5503017

我知道 访问这个https://api.baidu.com/json/tongji/v1/ReportService/getData
接口 但是我添加完参数访问后,返回的结果总是不正确的
返回结果 :{"header":{"desc":"system failure","failures":[{"code":8002,"message":"The Json message you provided is invalid.please check the request json sn:8000000000010","position":"_sys","content":"id:433887735864960"}],"status":3}}
不知道是什么原因,所以希望求源码 demo。net的

还是需要net 请求的方式
我的代码是 以下这样,但是报错误,不能用,有没有人可以看出哪里有问题?
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{

            string sContent = "site_id=*****&method=trend/latest/a&start_date=20180901&end_date=20180903&metrics=start_time,area,source,access_page,searchword&max_results=100&order=start_time,desc&offset=0";
            sContent = "{}";
            Response.Write(ResultData("https://api.baidu.com/json/tongji/v1/ReportService/getData", sContent, "", "").ContentResult);


        }
    }
    private static string _Input_Charset = "utf-8";// System.Text.Encoding.Default.BodyName;//
    public HttpResultDataModel ResultData(string sUrl, string sContent, string httpTopName, string httpTopValue, bool isPost = true, bool isXml = false)
    {
        HttpResultDataModel httpResult = new HttpResultDataModel();

        HttpWebRequest myReq = null;
        StreamReader readStream = null;
        HttpWebResponse myResp = null;
        try
        {
            Encoding encode = System.Text.Encoding.GetEncoding(_Input_Charset);//"utf-8"
            if (!isPost)
            {
                sUrl = sUrl + "?" + sContent;
            }
            myReq = (HttpWebRequest)WebRequest.Create(sUrl);
            myReq.Headers.Add("account_type", "1");
            myReq.Headers.Add("password", "******");
            myReq.Headers.Add("token", "*******");
            myReq.Headers.Add("username", "*******技术");
            if (isXml)
            { myReq.ContentType = "text/xml"; }
            else
            {
                //application/json
                myReq.ContentType = "application/json";
            }
            if (isPost)
            {
                byte[] arrB = encode.GetBytes(sContent);
                myReq.ContentLength = arrB.Length;
                myReq.Method = "POST";
                Stream outStream = myReq.GetRequestStream();
                outStream.Write(arrB, 0, arrB.Length);
                outStream.Close();
            }
            else
            {
                myReq.Method = "GET";
            }
            try
            {
                myResp = (HttpWebResponse)myReq.GetResponse();

            }
            catch (WebException ex)
            {
                myResp = (HttpWebResponse)ex.Response;
            }
            httpResult.HttpCode = (int)myResp.StatusCode;
            Stream ReceiveStream = myResp.GetResponseStream();
            readStream = new StreamReader(ReceiveStream, encode);// Encoding.UTF8
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string strRes = null;
            while (count > 0)
            {
                strRes += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            httpResult.ContentResult = strRes;
        }
        catch (Exception ex)
        {
            httpResult.ContentResult = ex.ToString();
        }
        finally
        {
            if (readStream != null)
            {
                readStream.Close();
            }
            if (myResp != null)
            {
                myResp.Close();
            }
        }
        return httpResult;
    }
}
public class HttpResultDataModel
{
    public string ContentResult { get; set; }
    public int HttpCode { get; set; }
}
it写代码的主页 it写代码 | 初学一级 | 园豆:73
提问于:2018-09-03 17:02
< >
分享
最佳答案
0

暂时没有找到解决办法,只有php版本,没有net版本,分数给你们对半分吧,也没人能解决了。

it写代码 | 初学一级 |园豆:73 | 2018-09-19 09:37
其他回答(3)
0

我觉得需要看一下文档,看起来是json数据格式不对啊

收获园豆:25
wdwwtzy | 园豆:114 (初学一级) | 2018-09-03 17:29

是,我一直没搞明白 这个接口具体要什么数据

支持(0) 反对(0) it写代码 | 园豆:73 (初学一级) | 2018-09-03 17:30

@糖果小宝~: 没有文档吗?

支持(0) 反对(0) wdwwtzy | 园豆:114 (初学一级) | 2018-09-03 17:30

@wdwwtzy: https://tongji.baidu.com/dataapi/file/TongjiApiFile.pdf 有个这个文档,也都按照上面的参数传了,结果还是这,可迷了。。。

支持(0) 反对(0) it写代码 | 园豆:73 (初学一级) | 2018-09-03 17:31

@wdwwtzy: 这个接口好像和目前通用的接口不太一样,不知道是不是我哪里写的不对

支持(0) 反对(0) it写代码 | 园豆:73 (初学一级) | 2018-09-03 17:32
0

貌似是你的请求json构造有问题。
参考错误码
https://cloud.baidu.com/doc/SEM/API/36.5C.E9.94.99.E8.AF.AF.E4.BB.A3.E7.A0.81.E8.AF.B4.E6.98.8E.html
参考示例(python)
https://www.cnblogs.com/w1570631036/p/7096966.html?utm_source=debugrun&utm_medium=referral

主要应该是要这个:
{"header": {"account_type": 1, "password": "", "token": "",
"username": ""},
"body": {"siteId": , "method": "visit/district/a", "start_date": start, "end_date": end,
"metrics": "pv_count,visitor_count,avg_visit_time"}}

收获园豆:25
Masako | 园豆:1893 (小虾三级) | 2018-09-03 18:05
0

请求参数 header和body 全部放到 http 请求的body中。
我之前也是被php的demo干扰了。
可以参考:
https://github.com/li-shaoke/StatisticadlData/blob/master/StatisticadlData/HttpHelper.cs

Eric-Lee | 园豆:228 (菜鸟二级) | 2019-04-30 09:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册