大家救急,HttpWebRequest.GetResponse()出错

景韦 2015-02-05 12:33:54
C#做的一个微信公众号,用了一年多了一直正常
最近突然出现问题,获取用户信息的时候报错

发现就是在读取一个URL的时候出错,而读取其他URL没问题
这个URL无论结果如何,返回的应该是一串JSON
我读取URL的代码如下

string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxx&secret=xxxxx&code=&grant_type=authorization_code";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
Stream resStream = myRes.GetResponseStream();
StreamReader strReader = new StreamReader(resStream);
string resStr = strReader.ReadToEnd();
Response.Write(resStr);

请大家帮忙看看这到底是怎么回事
万分感谢
...全文
503 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_30543179 2015-08-12
  • 打赏
  • 举报
回复
楼主是哪个方面的安全问题呢求指教吖。
江南小鱼 2015-02-05
  • 打赏
  • 举报
回复
lincolnandlinda 2015-02-05
  • 打赏
  • 举报
回复
你用浏览器直接请求看看能不能成功?
qzyf1992 2015-02-05
  • 打赏
  • 举报
回复
引用 11 楼 jewely 的回复:
[quote=引用 8 楼 qzyf1992 的回复:]

   string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxx&secret=xxxxx&code=&grant_type=authorization_code";
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);   
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            myReq.ProtocolVersion = HttpVersion.Version10;
            myReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36";
            HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
            Stream resStream = myRes.GetResponseStream();
            StreamReader strReader = new StreamReader(resStream);
            string resStr = strReader.ReadToEnd();
            Console.WriteLine(resStr);
            Console.Read();
效果如图
万分感谢 OK了 原来还是安全方面的问题啊 腾讯改设置,居然连通知也没有,我刚才还去问客服,他们都不清楚[/quote] 解决了就好结贴给分吧
景韦 2015-02-05
  • 打赏
  • 举报
回复
引用 8 楼 qzyf1992 的回复:

   string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxx&secret=xxxxx&code=&grant_type=authorization_code";
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);   
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            myReq.ProtocolVersion = HttpVersion.Version10;
            myReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36";
            HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
            Stream resStream = myRes.GetResponseStream();
            StreamReader strReader = new StreamReader(resStream);
            string resStr = strReader.ReadToEnd();
            Console.WriteLine(resStr);
            Console.Read();
效果如图
万分感谢 OK了 原来还是安全方面的问题啊 腾讯改设置,居然连通知也没有,我刚才还去问客服,他们都不清楚
qzyf1992 2015-02-05
  • 打赏
  • 举报
回复

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }
忘贴了
本拉灯 2015-02-05
  • 打赏
  • 举报
回复
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; 改成这个吧 SSL3协议是被改了吧。。
qzyf1992 2015-02-05
  • 打赏
  • 举报
回复

string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxxx&secret=xxxxx&code=&grant_type=authorization_code";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.ProtocolVersion = HttpVersion.Version10;
myReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36";
HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
Stream resStream = myRes.GetResponseStream();
StreamReader strReader = new StreamReader(resStream);
string resStr = strReader.ReadToEnd();
Console.WriteLine(resStr);
Console.Read();

效果如图
景韦 2015-02-05
  • 打赏
  • 举报
回复
引用 6 楼 qzyf1992 的回复:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
你好 我的代码第三行有这句话的
qzyf1992 2015-02-05
  • 打赏
  • 举报
回复
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
景韦 2015-02-05
  • 打赏
  • 举报
回复
引用 4 楼 findcaiyzh 的回复:
看着像网络没连上,是不是操作系统环境变了,想安装了防火墙啥的。
系统没有什么变化,我本机环境和服务器运行都是这个问题 这个URL访问也没问题,就是取不到内容 谢谢大家帮忙救急啊
宝_爸 2015-02-05
  • 打赏
  • 举报
回复
看着像网络没连上,是不是操作系统环境变了,想安装了防火墙啥的。
景韦 2015-02-05
  • 打赏
  • 举报
回复
引用 1 楼 lincolnandlinda 的回复:
你用浏览器直接请求看看能不能成功?
用浏览器看是可以的 无论URL参数怎样,都会有json返回 因为我把URL里的密钥隐藏了,所以得到的返回内容是错误提示 就算是{"errcode":41008,"errmsg":"missing code"}这样的错误信息 我在后面也会处理的 但现在问题是完全没办法取得这个url的返回内容 请求帮忙,或者大家也可以试着去读取一下这个URL,看看你们能读取吗? 还是我的环境配置有问题 谢谢

62,067

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧