模拟登录后,无法Post数据

hn_xiayanping 2010-12-09 04:35:45
我用HttpWebRequest 模拟登录后,要用Post的方法去查询数据 ,但是Post后,就会跳转到登录页面,如果不用Post,用Get就可以获取只能获取查询页面

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 600000 * Settings.Instance.TimeOut;
request.ReadWriteTimeout = 600000 * Settings.Instance.TimeOut;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)";
request.ContentType = ContentType;
request.Accept = Accept;
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = isPost ? "POST" : "GET";
if (isPost)
{
if (!string.IsNullOrEmpty(postData))
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
}
request.CookieContainer = cc;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
{
return reader.ReadToEnd();
}
}


...全文
111 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zr 2011-12-02
  • 打赏
  • 举报
回复
我加了 怎么不行啊 每次都会显示出登录界面
whowhen21 2010-12-09
  • 打赏
  • 举报
回复
不论Post 还是 Get 都需要,有的虽然是Get请求,但是需要你登陆信息的~~所以CookieContainer 加上去
whowhen21 2010-12-09
  • 打赏
  • 举报
回复
就是啊,保存可Cookie才行啦~~
hn_xiayanping 2010-12-09
  • 打赏
  • 举报
回复
自己搞定了

  request.CookieContainer = cc;

放到这里
 request.Method = isPost ? "POST" : "GET"; 
request.CookieContainer = cc;
if (isPost)
{
if (!string.IsNullOrEmpty(postData))
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧