主要思想是:先发送登录信息,接收返回的关于 session 的cookie值。
再次访问相应的页面时,将先前获得的cookie附加进去,服务器就能识别。在windows form中我没有试过。你自已再改改吧。
再不行,我晚上帮你看看,现在没时间。

解决方案 »

  1.   

    我也是这样想的,那个
    string strLogin = Login("http://nnsky.com/bbs/login.asp", "Action=chklogin.asp&userName=sqhua&Password=xx") ;里的Action和后边的内容都是直接看login.asp页面里的Form内容的TextBox的ID而定的吧。那如果要模拟填写网页里的输入框(input),是不是都可以这么做(模拟用IE来登录后发言)你有时间就帮试一下,我现在只是能取到页面内容,而不能用户登录
      

  2.   

    以下代码在windows 工程中测试成功:
    Form1.cs:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Net;
    using System.Text;
    using System.IO;namespace MyPhotos
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    protected static string cookieheader; public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(32, 128);
    this.textBox1.Multiline = true;
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(440, 192);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(512, 333);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.textBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    //string strResult; //string strLogin = Login("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action=&USERID=&Password=") ;
    string strLogin = Login("http://nnsky.com/bbs/chklogin.asp?userName=spring_o&Password=een7q2ss34&submit=登 陆","") ;
    //strResult = getPage("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action=&data=") ; // output the result
    //textBox1.Text = strResult;
    textBox1.Text = strLogin;
    }
    public static string Login(String url, String paramList) 
    {
    HttpWebResponse res = null;
    string strResult=""; try 
    { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.AllowAutoRedirect = true;
    CookieContainer cookieCon = new CookieContainer();
    req.CookieContainer = cookieCon; StringBuilder UrlEncoded = new StringBuilder();
    Char[] reserved = {'?', '=', '&'};
    byte[] SomeBytes = null; if (paramList != null) 
    {
    int i=0, j;
    /*
     while(i<paramList.Length)
    {
    j=paramList.IndexOfAny(reserved, i);
    if (j==-1)
    {
    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
    break;
    }
    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
    UrlEncoded.Append(paramList.Substring(j,1));
    i = j+1;
    } */
    UrlEncoded = UrlEncoded.Append(paramList);
    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
    req.ContentLength = SomeBytes.Length;
    Stream newStream = req.GetRequestStream();
    newStream.Write(SomeBytes, 0, SomeBytes.Length);
    newStream.Close();

    else 
    {
    req.ContentLength = 0;
    }
    res = (HttpWebResponse)req.GetResponse();
    cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url)); Stream ReceiveStream = res.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader sr = new StreamReader( ReceiveStream, encode );
    Char[] read = new Char[256];
    int count = sr.Read( read, 0, 256 );
    while (count > 0) 
    {
    String str = new String(read, 0, count);
    strResult += str;
    count = sr.Read(read, 0, 256);
    }

    catch(Exception e) 
    {
    strResult = e.ToString();

    finally 
    {
    if ( res != null ) 
    {
    res.Close();
    }
    } return strResult;
    }
    public static string getPage(String url, String paramList) 
    {
    HttpWebResponse res = null;
    string strResult = ""; try 
    { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.KeepAlive = true;
    req.ContentType = "application/x-www-form-urlencoded";
    CookieContainer cookieCon = new CookieContainer();
    req.CookieContainer = cookieCon;
    req.CookieContainer.SetCookies(new Uri(url),cookieheader);
    StringBuilder UrlEncoded = new StringBuilder();
    Char[] reserved = {'?', '=', '&'};
    byte[] SomeBytes = null; if (paramList != null) 
    {
    int i=0, j;
    /*
    while(i<paramList.Length)
    {
    j=paramList.IndexOfAny(reserved, i);
    if (j==-1)
    {
    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
    break;
    }
    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
    UrlEncoded.Append(paramList.Substring(j,1));
    i = j+1;
    }
    */
    UrlEncoded = UrlEncoded.Append(paramList);
    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
    req.ContentLength = SomeBytes.Length;
    Stream newStream = req.GetRequestStream();
    newStream.Write(SomeBytes, 0, SomeBytes.Length);
    newStream.Close();

    else 
    {
    req.ContentLength = 0;
    }
    res = (HttpWebResponse)req.GetResponse();
    Stream ReceiveStream = res.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader sr = new StreamReader( ReceiveStream, encode );
    Char[] read = new Char[256];
    int count = sr.Read( read, 0, 256 );
    while (count > 0) 
    {
    String str = new String(read, 0, count);
    strResult += str;
    count = sr.Read(read, 0, 256);
    }

    catch(Exception e) 
    {
    strResult = e.ToString();

    finally 
    {
    if ( res != null ) 
    {
    res.Close();
    }
    } return strResult;
    } }
    }要解决的问题: HttpUtility.urlEncode() 我找不到对应的可在WINFORM中使用的类。 返回结果中的中文字符未能正确显示。WINFORM我还不熟,你自已再试试吧。
      

  3.   

    把“utf-8”换成“GB2312”就可以显示中文了