最近在研究校内 我用想写一个C#登陆校内的小程序 就是我想用POST的方式提交用户名跟密码 然后返回登陆成功或失败 但是返回来的数据并没有先生成功也没有显示失败
主要代码如下:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "&email= " + this.textBox1.Text;
postData += ("&password= " + this.textBox2.Text); byte[] data = encoding.GetBytes(postData); // Prepare web request...
HttpWebRequest objHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://login.xiaonei.com/Login.do"); objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
objHttpWebRequest.ContentLength = data.Length;
Stream newStream = objHttpWebRequest.GetRequestStream(); // 发送数据.
newStream.Write(data, 0, data.Length);
newStream.Close(); // 得到数据
HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
StreamReader reader = new StreamReader(objHttpWebResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
this.textBox3.Text = content;
请问我什么地方出问题了,或者是还有什么更好的方法最好把代码也贴出来,谢谢
理论上讲 这个代码是没有问题的 但是却没有成功 麻烦一下 大家负责任点 实践一下 再回答问题
主要代码如下:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "&email= " + this.textBox1.Text;
postData += ("&password= " + this.textBox2.Text); byte[] data = encoding.GetBytes(postData); // Prepare web request...
HttpWebRequest objHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://login.xiaonei.com/Login.do"); objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
objHttpWebRequest.ContentLength = data.Length;
Stream newStream = objHttpWebRequest.GetRequestStream(); // 发送数据.
newStream.Write(data, 0, data.Length);
newStream.Close(); // 得到数据
HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
StreamReader reader = new StreamReader(objHttpWebResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
this.textBox3.Text = content;
请问我什么地方出问题了,或者是还有什么更好的方法最好把代码也贴出来,谢谢
理论上讲 这个代码是没有问题的 但是却没有成功 麻烦一下 大家负责任点 实践一下 再回答问题
public static bool login(string user, string passwd,bool isuseproxy,WebProxy proxy)
{
string text1 = "http://login.xiaonei.com/Login.do";
string[] textArray1 = new string[5] { "email=", user, "&password=", passwd, "&origURL=http%3A%2F%2Fwww.xiaonei.com%2FSysHome.do&submit=%E7%99%BB%E5%BD%95" };
string text2 = string.Concat(textArray1);
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(text1);
request1.Method = "POST";
request1.KeepAlive = true;
request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1";
request1.ContentType = "application/x-www-form-urlencoded";
request1.AllowAutoRedirect = true;
if (proxy != null && isuseproxy != false)
{
request1.Proxy = proxy;
}
request1.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
CookieContainer container1 = new CookieContainer();
request1.CookieContainer = container1;
Stream stream1 = request1.GetRequestStream();
StreamWriter writer1 = new StreamWriter(stream1, Encoding.ASCII);
writer1.Write(text2);
writer1.Close();
stream1.Close();
HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
Program.cookie = request1.CookieContainer.GetCookieHeader(new Uri("http://login.xiaonei.com/Login.do"));
Encoding encoding1 = Encoding.GetEncoding("utf-8");
Stream stream2 = response1.GetResponseStream();
StreamReader reader1 = new StreamReader(stream2, encoding1);
StringBuilder builder1 = new StringBuilder();
while (reader1.Peek() != -1)
{
builder1.Append(reader1.ReadLine());
}
string text3 = builder1.ToString();
request1.Abort();
Match match1 = Regex.Match(text3, "<title>.*</title>");
if ((match1.Length >= 1) && (match1.Value != "<title>\u6821\u5185\u7f51 - \u767b\u5f55</title>"))
{
Match match2 = Regex.Match(text3, "id=[0-9]{8,9}.*\u9996\u9875");
if (match2.Length >= 1)
{
Match match3 = Regex.Match(match2.Value, "[0-9]{8,9}");
if (match3.Length >= 1)
{
Program.selfID = match3.Value;
}
}
return true;
}
return false;
}
public void Login(string UserName, string UserPwd, string LoginUrl, out string Txt)
{
try
{
//定义Cookie容器
CookieContainer CookieArray = new CookieContainer(); //创建Http请求
HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(LoginUrl); LoginHttpWebRequest.UseDefaultCredentials = true;
LoginHttpWebRequest.Proxy = this.getProxy(); //登录数据
string LoginData = "email=" + UserName + "&password=" + UserPwd;
//数据被传输类型
LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
//数据长度
LoginHttpWebRequest.ContentLength = LoginData.Length;
//数据传输方法 get或post
LoginHttpWebRequest.Method = "POST";
//设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray
LoginHttpWebRequest.CookieContainer = CookieArray;
//获取登录数据流
Stream myRequestStream = LoginHttpWebRequest.GetRequestStream();
//StreamWriter
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.Default);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Write(LoginData); //关闭打开对象
myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse
HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection
myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); Txt = myStreamReader.ReadToEnd(); //把数据从HttpWebResponse的Response流中读出
myStreamReader.Close(); myResponseStream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
你的代码来自http://blog.csdn.net/springfeng2008/archive/2009/06/03/4238475.aspx
希望以后大家遇到同样的问题的时候 不会想我一样走了这么多歪路