http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3918|回复: 2

[HttpHelper] HTTP助手类向钉钉发送请求返回“操作繁忙”

[复制链接]
发表于 2017-4-14 11:28:03 | 显示全部楼层 |阅读模式
最近在开发钉钉服务端功能,钉钉的服务端开发与微信套路几乎一致,都是先拿到ACCESS_TOKEN后再按照接口要求请求。

这里我遇到了一个很奇怪的问题,在获取钉钉ACCESS_TOKEN时使用的是开发助手生成的GET访问代码,一切正常。
拿到ACCESS_TOKEN后用开发助手生成的POST访问代码 访问钉钉接口时却报错了,返回“操作繁忙”,代码我贴在下面:
根据钉钉要求,我将ContentType设置为application/json,其他都是代码生成,没有改动

[C#] 纯文本查看 复制代码
public static string Post(string url, string postData, string cookie = "")
    {
        HttpHelper http = new HttpHelper();
        HttpItem item = new HttpItem()
        {
            URL = url,//URL     必需项    
            Method = "post",//URL     可选项 默认为Get   
            IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
            Cookie = cookie,//字符串Cookie     可选项   
            Referer = "",//来源URL     可选项   
            Postdata = postData,//Post数据     可选项GET时不需要写   
            Timeout = 10000,//连接超时时间     可选项默认为100000    
            ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
            UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
            ContentType = "application/json",//返回类型    可选项有默认值   
            Allowautoredirect = false,//是否根据301跳转     可选项     
            ResultType = ResultType.String
        };
        HttpResult result = http.GetHtml(item);
        string html = result.Html;
        return html;
    }

多次尝试无果后,我改用HttpWebRequest类一次成功,我把代码帖在下面:
[C#] 纯文本查看 复制代码
public static string Post_JSON(string url, string postData, string cookie = "")
    {
        string result = "";

        byte[] bt = System.Text.Encoding.UTF8.GetBytes(postData);
        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));
        req.Timeout = 3000;
        req.KeepAlive = false; //防止链接中断无响应
        req.ProtocolVersion = System.Net.HttpVersion.Version10;
        req.Method = "POST";
        req.ContentType = "application/json";
        req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";

        if (!string.IsNullOrEmpty(cookie))
        {
            req.Headers["Cookie"] = cookie;
        }

        try
        {
            using (System.IO.Stream streamReq = req.GetRequestStream())
            {
                streamReq.Write(bt, 0, bt.Length);
                streamReq.Close();
                streamReq.Dispose();

                System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                    reader.Close();
                    reader.Dispose();
                    res.Close();
                    req.Abort();
                }
            }
        }
        catch
        {
            ConsoleColor Color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(snow.时间处理(2, true) + " " + url + " POST发送失败。");
            Console.ForegroundColor = Color;
            result = "";
        }

        return result;
    }

百度搜索了一下,之前已经有人遇到类似的问题,最后是“放弃WebClient类改用HttpWebRequest类”解决的。
所以请教一下苏飞老师,可能是哪里出错了?


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2017-4-14 13:07:22 | 显示全部楼层
写法有区别啊
req.KeepAlive = false; //防止链接中断无响应
req.ProtocolVersion = System.Net.HttpVersion.Version10;
这两个参数你只在下面方法写了,并没有在Httphelper中配置,
 楼主| 发表于 2017-4-15 08:26:36 | 显示全部楼层
站长苏飞 发表于 2017-4-14 13:07
写法有区别啊
req.KeepAlive = false; //防止链接中断无响应
req.ProtocolVersion = System.Net.HttpV ...

谢谢站长回复!我再去试一下
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-29 01:59

© 2014-2021

快速回复 返回顶部 返回列表