最近在做爬虫程序,在post提交模拟登录时遇到了些问题,一些网站是正常的,但有些怎么测试也是出错,希望高手能帮忙看一下,谢谢。
代码如下:是登录百度贴吧

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Specialized;

namespace TestHttp


{
public partial class frmBaiduTB : Form

{
const string C_LoginUrl = "https://passport.baidu.com/?login";
CookieContainer m_cc = new CookieContainer();

public frmBaiduTB()

{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)

{
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(C_LoginUrl);
httpReq.Method = "POST";
httpReq.CookieContainer = m_cc;
httpReq.Accept = "*/*";
httpReq.Referer = "http://passport.baidu.com/?login";
httpReq.ContentType = "application/x-www-form-urlencoded";
httpReq.AllowAutoRedirect = false;

httpReq.Headers.Add("Accept-Encoding", "gzip, deflate");
httpReq.Headers.Add("Accept-Language", "zh-cn");
httpReq.Headers.Add("Cache-Control", "no-cache");
httpReq.Headers.Add("UA-CPU", "x86");
httpReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)";

string strPostData = GetPostData();
byte[] bsInData = Encoding.GetEncoding("utf-8").GetBytes(strPostData);
httpReq.ContentLength = bsInData.Length;

Stream inputStream = httpReq.GetRequestStream();
inputStream.Write(bsInData, 0, bsInData.Length);
inputStream.Close();
inputStream.Dispose();

HttpWebResponse httpRes = null;

try

{
//此处总是出现错误,{"基础连接已经关闭: 连接被意外关闭。"}System.Exception {System.Net.WebException}
//这是怎么回事啊?
httpRes = (HttpWebResponse)httpReq.GetResponse();
}
catch (WebException ex)

{
httpRes = (HttpWebResponse)ex.Response;
MessageBox.Show( ex.Message );
return;
}

Stream outStream = httpRes.GetResponseStream();
StreamReader sr = new StreamReader(outStream);

MessageBox.Show(sr.ReadToEnd());
sr.Close();

string strLocation = "";
string strCookie = "";

if (httpRes.StatusCode == HttpStatusCode.Redirect)

{
strLocation = httpRes.Headers["Location"];
strCookie = httpRes.Headers.Get("Set-Cookie");
}
}

private string GetPostData()

{
string strRtn = "";

NameValueCollection nvc = new NameValueCollection();
nvc.Add("aid","");
nvc.Add("more_param","");
nvc.Add("need_coin","");
nvc.Add("need_pay","");
nvc.Add("next_target","");
nvc.Add("password", txtPwd.Text);
nvc.Add("pay_method","");
nvc.Add("psp_tt","0");
nvc.Add("return_method","get");
nvc.Add("return_type","");
nvc.Add("safeflg","0");
nvc.Add("skip_ok","");
nvc.Add("tpl","tb");
nvc.Add("tpl_ok","");
nvc.Add("u","http://tieba.baidu.com/");
nvc.Add("username", txtUserName.Text);
nvc.Add("verifycode","");

foreach (string ie in nvc)

{
strRtn += ie + "=" + nvc[ie] + "&";
}
if (strRtn.EndsWith("&"))
strRtn = strRtn.Substring(0, strRtn.Length - 1);
return strRtn; ;
}
}
}
