HttpWebRequest-百度贴吧登录错误-基础连接已经关闭,连接被意外关闭

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

代码如下:是登录百度贴吧

 

ContractedBlock.gif ExpandedBlockStart.gif 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class frmBaiduTB : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
const string C_LoginUrl = "https://passport.baidu.com/?login";
        CookieContainer m_cc 
= new CookieContainer();

        
public frmBaiduTB()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
private void btnLogin_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//此处总是出现错误,{"基础连接已经关闭: 连接被意外关闭。"}System.Exception {System.Net.WebException}
                
//这是怎么回事啊?
                httpRes = (HttpWebResponse)httpReq.GetResponse();
            }

            
catch (WebException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                strLocation 
= httpRes.Headers["Location"];
                strCookie 
= httpRes.Headers.Get("Set-Cookie");
            }
           
        }


        
private string GetPostData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                strRtn 
+= ie + "=" + nvc[ie] + "&";
            }

            
if (strRtn.EndsWith("&"))
                strRtn 
= strRtn.Substring(0, strRtn.Length - 1);
            
return strRtn; ;
        }

    }

}

 

 

 

转载于:https://www.cnblogs.com/domlfirmer/archive/2009/11/13/1602781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值