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.Net;
using System.IO;namespace m百度
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            string url = "http://wappass.baidu.com/passport/";
            string postData = "login_username="+textBox1.Text+"&login_loginpass="+textBox2.Text+"&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=&login_start_time=1359431843&tpl=&tn=&pu=&ssid=&from=&bd_page_type=&uid=1359431843588_646&login_username_input=0&type=";
            byte[] byteRequest = Encoding.Default.GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = cookieContainer;
            request.Referer = url;
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteRequest.Length;  //附加信息长度
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
            request.Method = "POST";            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();  //转换为数据流
            StreamReader reader = new StreamReader(stream);
            string html = reader.ReadToEnd();   //通过StreamReader类读取流
            html = richTextBox1.Text;
            reader.Close();
            stream.Close();        }        public CookieContainer cookieContainer { get; set; }
    }
}
求解决,代码有两处出错,不知道为什么,寻找路过的高手帮忙解决
小弟我不甚感激!!!!!c#login模拟登录

解决方案 »

  1.   


                string url = "http://wappass.baidu.com/passport/";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                string postData = "login_username=" + textBox1.Text + "&login_loginpass=" + textBox2.Text + "&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=&login_start_time=1359431843&tpl=&tn=&pu=&ssid=&from=&bd_page_type=&uid=1359431843588_646&login_username_input=0&type=";
                byte[] byteRequest = Encoding.Default.GetBytes(postData);
                request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.CookieContainer = cookieContainer;
                request.Referer = url;
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteRequest.Length;  //附加信息长度
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
                request.Method = "POST";            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();  //转换为数据流
                StreamReader reader = new StreamReader(stream);
                string html = reader.ReadToEnd();   //通过StreamReader类读取流
                html = richTextBox1.Text;
                reader.Close();
                stream.Close();
      

  2.   

    这里有指定:request.ContentLength = byteRequest.Length;  //附加信息长度但是你没有Stream.Writer        Steam.Write(byteRequest, 0, byteRequest.Length);请自行调试将该代码加入到正确位置.
      

  3.   

    string url = "http://wappass.baidu.com/passport/";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                string postData = "login_username=" + textBox1.Text + "&login_loginpass=" + textBox2.Text + "&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=&login_start_time=1359431843&tpl=&tn=&pu=&ssid=&from=&bd_page_type=&uid=1359431843588_646&login_username_input=0&type=";
                byte[] byteRequest = Encoding.Default.GetBytes(postData);
                request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.CookieContainer = cookieContainer;
                request.Referer = url;
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = byteRequest.Length;  //附加信息长度
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
                request.Method = "POST";
                Stream newStream = request.GetRequestStream();
                // Send the data.
                newStream.Write(byteRequest, 0, byteRequest.Length);
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                string html = reader.ReadToEnd();   //通过StreamReader类读取流
                html = richTextBox1.Text;
                reader.Close();
                stream.Close();
      

  4.   

    1.url 和 Request 定义出现混乱。2.没有写入POST 的数据。
      

  5.   

    你定义url的位置不对吧。要放在上面啊,
    而且你定义了2次request了
      

  6.   

    個人建議HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 這語句出現了兩次把第一次的刪掉就可以了