C# HttpWebRequest的问题 10
网上找到的代码,运行时不能正常登录,是不是提交的数据格式变化了?要怎么改另外,怎么分析网页postdata所需的格式?分有点少,没分了,不好意思FORM上放一个BUTTO...
网上找到的代码,运行时不能正常登录,是不是提交的数据格式变化了?要怎么改
另外,怎么分析网页post data所需的格式?
分有点少,没分了,不好意思
FORM上放一个BUTTON,两个TEXTBOX,一个WEBBROWSER
----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace BaiDu_Logon
{
public partial class FormMain : Form
{
CookieContainer cc = new CookieContainer();
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string BAIDU_Url = "http://passport.baidu.com/?login";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(BAIDU_Url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = false;
byte[] postData = System.Text.Encoding.Default.GetBytes("username=" + this.textBox1.Text + "&password=" + this.textBox2.Text + "&mem_pass=");
req.ContentLength = postData.Length;
req.CookieContainer = cc;
using (Stream sm = req.GetRequestStream())
{
sm.Write(postData, 0, postData.Length);
}
string strResult;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
res.Cookies = cc.GetCookies(req.RequestUri);
using (Stream smRes = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(smRes, System.Text.Encoding.Default))
{
strResult = sr.ReadToEnd();
}
}
res.Close();
req = (HttpWebRequest)WebRequest.Create("http://zhidao.baidu.com/q?ct=17&rn=25&lm=393738&tn=ikpsask2");
req.CookieContainer = cc;
res = (HttpWebResponse)req.GetResponse();
using (Stream smRes = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(smRes, System.Text.Encoding.Default))
{
strResult = sr.ReadToEnd();
}
}
res.Close();
string filename = Path.GetTempFileName();
using (StreamWriter sw = new StreamWriter(filename, false, System.Text.Encoding.Default))
{
sw.Write(strResult);
}
this.webBrowser1.Navigate("file://" + filename);
}
}
} 展开
另外,怎么分析网页post data所需的格式?
分有点少,没分了,不好意思
FORM上放一个BUTTON,两个TEXTBOX,一个WEBBROWSER
----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace BaiDu_Logon
{
public partial class FormMain : Form
{
CookieContainer cc = new CookieContainer();
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string BAIDU_Url = "http://passport.baidu.com/?login";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(BAIDU_Url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = false;
byte[] postData = System.Text.Encoding.Default.GetBytes("username=" + this.textBox1.Text + "&password=" + this.textBox2.Text + "&mem_pass=");
req.ContentLength = postData.Length;
req.CookieContainer = cc;
using (Stream sm = req.GetRequestStream())
{
sm.Write(postData, 0, postData.Length);
}
string strResult;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
res.Cookies = cc.GetCookies(req.RequestUri);
using (Stream smRes = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(smRes, System.Text.Encoding.Default))
{
strResult = sr.ReadToEnd();
}
}
res.Close();
req = (HttpWebRequest)WebRequest.Create("http://zhidao.baidu.com/q?ct=17&rn=25&lm=393738&tn=ikpsask2");
req.CookieContainer = cc;
res = (HttpWebResponse)req.GetResponse();
using (Stream smRes = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(smRes, System.Text.Encoding.Default))
{
strResult = sr.ReadToEnd();
}
}
res.Close();
string filename = Path.GetTempFileName();
using (StreamWriter sw = new StreamWriter(filename, false, System.Text.Encoding.Default))
{
sw.Write(strResult);
}
this.webBrowser1.Navigate("file://" + filename);
}
}
} 展开
1个回答
展开全部
改为:req = (HttpWebRequest)WebServer.Create("http://zhidao.baidu.com/q?ct=17&rn=25&lm=393738&tn=ikpsask2");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询