string loginStr=string.Empty;
string getDate=string.Empty,postDate=string.Empty;
byte[] sendByte;
HttpWebRequest request=null;
HttpWebResponse response=null;
CookieContainer container=new CookieContainer();
CookieCollection collection=null;
Stream stream;
string cookieStr=string.Empty;
StreamReader streamReader;
string returnStr=string.Empty;
try{
loginStr=@"http://www.baidu.com/";
getDate=@"wd=aaaaaaaaaa";
sendByte=Encoding.UTF8.GetBytes(getDate);
request=(HttpWebRequest)WebRequest.Create(loginStr);
request.Method="GET";
request.ContentType="application/x-www-form-urlencoded";
request.ContentLength=sendByte.Length;
request.AllowAutoRedirect=false;
request.CookieContainer=container;
//request.CookieContainer.SetCookies(new Uri("http://www.baidu.com"),);
request.KeepAlive=true;
stream=request.GetRequestStream();
stream.Write(sendByte,0,sendByte.Length);
stream.Close();





response=(HttpWebResponse)request.GetResponse();
response.Cookies=request.CookieContainer.GetCookies(request.RequestUri);
collection=response.Cookies;
cookieStr=request.CookieContainer.GetCookieHeader(request.RequestUri);
streamReader=new StreamReader(response.GetResponseStream(),Encoding.GetEncoding("gb2312"));
returnStr=streamReader.ReadToEnd();
response.Close();
streamReader.Close();
}catch(Exception)
{
returnStr="22";
}
return returnStr;

}
大家帮我看看怎么看看哪里错了

解决方案 »

  1.   

    string loginStr = string.Empty;
    string getDate = string.Empty, postDate = string.Empty;
    byte[] sendByte;
    HttpWebRequest request = null;
    HttpWebResponse response = null;
    CookieContainer container = new CookieContainer();string cookieStr = string.Empty;
    StreamReader streamReader;
    string returnStr = string.Empty;loginStr = @"http://www.baidu.com/s?";
    getDate = @"wd=aaaaaaaaaa";
    loginStr += getDate;
    sendByte = System.Text.UnicodeEncoding.GetEncoding("GB2312").GetBytes(getDate);
    request = (HttpWebRequest)WebRequest.Create(loginStr);
    request.Method = "GET";response = (HttpWebResponse)request.GetResponse();streamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
    returnStr = streamReader.ReadToEnd();
    response.Close();
    streamReader.Close();
      

  2.   

    谢谢 net_lover ,还有是不是,用GET提交的也可以改成用POST提交,如果改成POST要怎么改。
      

  3.   

    Post的例子
    http://dotnet.aspx.cc/article/00000000-0000-0000-0000-00000000000c/read.aspx
      

  4.   

    悲剧,哪里错了
    public string PostHtml(string targetURL,string postDate,string requestEncoding,string responseEncoding)
    {
    this.targetURL=targetURL;
    this.postDate=postDate;
    this.requestEncoding=requestEncoding;
    this.responseEncoding=responseEncoding;


    try
    {
    this.encoding=Encoding.GetEncoding(this.requestEncoding);
    this.postBytes=this.encoding.GetBytes(this.postDate);


    this.request=(HttpWebRequest)WebRequest.Create(this.targetURL);
    this.request.CookieContainer=this.container;


    this.request.ProtocolVersion=HttpVersion.Version10;
    this.request.UserAgent="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; CIBA; .NET4.0C; .NET4.0E)";
    this.request.Accept="image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
    this.request.Timeout=100000;



    this.request.KeepAlive=true;
    this.request.ContentType="application/x-www-form-urlencoded; charset=UTF-8";
    //this.request








    //this.request.ContentType="application/x-www-form-urlencoded";
    this.request.ContentLength=this.postBytes.Length;
    this.request.Method="POST";

    Stream requestStream=this.request.GetRequestStream();
    requestStream.Write(this.postBytes,0,this.postBytes.Length);
    requestStream.Close();

    this.response=(HttpWebResponse)this.request.GetResponse();
    Stream responseStream=this.response.GetResponseStream();
    this.encoding=Encoding.GetEncoding(this.responseEncoding);
    StreamReader responseStreamReader=new StreamReader(responseStream,this.encoding);
    this.returnStr=responseStreamReader.ReadToEnd();
    responseStreamReader.Close();
    responseStream.Close();




    //request.AllowAutoRedirect=true;
    //request.KeepAlive = true;
    //request.Headers.Add("Accept-Language", "zh-cn");





    }catch(Exception e)
    {
    returnStr=e.Message;
    }
    return this.returnStr;
    }void Button1Click(object sender, System.EventArgs e)
    {
    //this.textBox1.Text=AutoLogin.PostData("s?wd=aaaaaaaaaaaaa","POST","http://www.baidu.com/");
    //this.pictureBox1.Image=AutoLogin.GetImage(@"http://reg.qq.com/");
    AutoLogin test=new AutoLogin();
    MessageBox.Show(test.PostHtml(this.textBox1.Text,this.textBox2.Text,this.textBox3.Text,this.textBox4.Text));
    //MessageBox.Show(AutoLogin.loginHtml());
    }测试对象是: http://user.qukanshu.com/userlogin.php?jumpurl=http%3A//book.qukanshu.com/
      

  5.   

    this.textBox1.Text="http://user.qukanshu.com/api/userlogin.php"; this.textBox2.Text="username=账号&password=密码";