HttpWebRequest登录某https网站失败的问题

wanderer_hh 2012-02-23 11:42:17
登录的代码都是用网上找的代码改的,以前孔网是http,程序一直运行正常,不知道什么时候改成https了,然后就不能登录

在网上查了很多文章,又用周公的博客里提供他写的相应HttpWebRequest访问类也是不行,同样的代码登录百度就能成功

参数是用httpWatch跟踪的,请高手指点,代码可直接试,我建的测试帐户是:用户名:mytestbook 密码:testbook


调用代码:

string url = "https://user.kongfz.com/member/login.php";
string postData = "username=mytestbook&password=testbook&act=login&returnUrl=http%3A%2F%2Fwww.kongfz.com&sb=%E7%99%BB%E5%BD%95";
string retStr = HttpWebResponseUtility.CreatePostHttpResponse(url, postData, null, null, null, null);


这是周公博客上的HTTP请求辅助类,我改了下返回值

/// <summary>
/// /// 有关HTTP请求的辅助类
/// </summary>
public class HttpWebResponseUtility
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

public static string CreatePostHttpResponse(string url, string postData, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
if (requestEncoding == null)
{
requestEncoding = Encoding.UTF8;
}
HttpWebRequest request = null; //如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
{
request = WebRequest.Create(url) as HttpWebRequest;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(userAgent))
{
request.UserAgent = userAgent;
}
else
{
request.UserAgent = DefaultUserAgent;
}
if (timeout.HasValue)
{
request.Timeout = timeout.Value;
}
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
byte[] data = requestEncoding.GetBytes(postData);
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();

Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, requestEncoding == null ? Encoding.UTF8 : requestEncoding);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();

request.Abort();
httpWebResponse.Close();

return html;
}

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
}
...全文
516 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liji007 2012-02-27
  • 打赏
  • 举报
回复
我想应该是POST参数不全或是cookie作用域的问题,我之前作soso问问自动登录也遇到过类型情况!

有一种方法你可以参考一下,不用POST数据,改用webbrower控件来模拟鼠标点击事件试试
周公 2012-02-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wanderer_hh 的回复:]
没人知道吗?求高手指点
[/Quote]
你要跟踪确认提交了哪些参数,有很多网站在提交的时候会通过js对参数进行处理,甚至中间还有跳转页面。

我在处理登录163.com时也出现过类似的问题,最后一步步跟踪才发现问题所在。
wanderer_hh 2012-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jiuhexuan 的回复:]
http://hi.baidu.com/sdwffzlll/blog/item/051066d3b410e9379a502723.html
[/Quote]
抱歉,那天没看到这个回复,但这篇文章我搜索时也看到的,解决不了问题
蔡袅 2012-02-24
  • 打赏
  • 举报
回复
按说没问题呀,CheckValidationResult也有了。
jiuhexuan 2012-02-24
  • 打赏
  • 举报
回复
http://zhoufoxcn.blog.51cto.com/792419/561934
jiuhexuan 2012-02-24
  • 打赏
  • 举报
回复
http://hi.baidu.com/sdwffzlll/blog/item/051066d3b410e9379a502723.html
wanderer_hh 2012-02-24
  • 打赏
  • 举报
回复
没人知道吗?求高手指点
wanderer_hh 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xx_mm 的回复:]
按说没问题呀,CheckValidationResult也有了。
[/Quote]
是啊,这代码本身应该是没问题的,因为登录同样是https的百度就能成功,但为什么登录孔夫子网就不行呢?

如果对这方面有研究请帮我试一下,我已经卡了一周多了,自己反复修改测试,在网上查了N多的东西,还是弄不明白
wanderer_hh 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jiuhexuan 的回复:]
http://zhoufoxcn.blog.51cto.com/792419/561934
[/Quote]
周公的这篇文章我在网上找答案的时候也看到了,我前面那个HTTP请求的辅助类就是用他的
但是不管用他那个类还是用我以前的登录都失败,同样用这两个类登录百度就都能成功,都是https的网站,真是弄不明白差在哪里

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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