using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CookieContainer _CookidContainer = new CookieContainer();
try
{
//模拟登陆
string name = "test7474@126.com";
string pwd = "qwer1234";
//ASCIIEncoding encoding = new ASCIIEncoding();
Encoding encoding = Encoding.GetEncoding("gb2312");
string postData = "user=" + name;
postData += "&password=" + pwd;
//postData = HttpUtility.UrlEncode(postData);
//byte[] data = encoding.GetBytes(postData);
//byte[] data = Encoding.ASCII.GetBytes(postData);
byte[] data =Encoding.Default.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("http://reg.163.com/logins.jsp");
myRequest.Accept = "*/*";
myRequest.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.126.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)";
myRequest.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
myRequest.Timeout = 3000;
myRequest.KeepAlive = true;
myRequest.CookieContainer=_CookidContainer;
myRequest.Credentials = CredentialCache.DefaultCredentials;
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//StreamReader reader =
string content = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
string cookie = myResponse.Headers.Get("Set-Cookie");
Response.Write(content);
}
catch (Exception ex)
{
Response.Write(ex.Message + ex.Source);
}
}
}
用户名和密码再网页中能登陆,但程序返回的确是 用户名或密码错误,请各位高手指点谢谢。。。。。
换个Encoding,比如UTF8试试?