!用HttpRequest进行HTTP Post操作时遇到一些问题

zju_mick 2005-08-01 05:13:13
我用HttpRequest进行HTTP Post操作时遇到一些问题:
背景:
1. 该网站是基于.NET Passport身份验证的(PPE环境),我需要HTTP Post数据的页面(以下称页面A)需要身份验证后方能登陆。
2. 我通过浏览器操作,进行登陆,并且选择“记住用户名与密码”,从此以后不再需要登陆即可访问页面A

我的行为:
我写了段C#代码向页面A进行HTTP Post操作,发现返回的html是未经验证的
我已经作了以下尝试:
1. 初始化HttpRequest时通过调用wininet.dll里面的API: InternetGetCookie函数获取某URI的Cookies,然后绑定到HttpRequest,但发现无法解决问题。我接着跟踪了IE临时文件夹下cookie文件的变化,发现有两个cookie文件被用到:一个是本网站,一个是passport-ppe.com,我于是加载了该两个站点在本地的cookie,发现加载的cookie不完全(比后来sniff抓包发现的cookie少),运行结果仍然是没有登陆。
2. 接着,我又尝试了用sniff工具(netmon in windows server 2003)监听了本机的http get and post,记录了一些cookie值,手工加载到HttpRequest,但仍然没有效果

我怀疑的错误:
1. InternetGetCookie函数获取cookie有问题,但我试过了自己写的测试网站,没问题,但针对这个问题的开发网站,行为有点异常。
2. 模拟IE行为到底需要哪些cookie?因为现在用浏览器直接访问已经无需登陆。

请教一下:怎样处理这个HttpRequest无法处理.NET Passport登陆的问题

我的代码:
static void Main(string[] args)
{
TryPostListing();
}

private static void TryPostListing()
{
string url = "http://greenridge/bb/postlisting.aspx";

string indata = "name=Mick&listing=nothing";
string outdata = "";
CookieContainer myCookieContainer = GetCookieContainerForUrl(new Uri(url));

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = myCookieContainer;

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
myStreamWriter.Close();
myRequestStream.Close();

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer = myCookieContainer;
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpWebResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);

StreamWriter sw = new StreamWriter("test.htm", false, Encoding.GetEncoding("gb2312"));
sw.WriteLine(outdata);
sw.Close();

}

[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookie(
string url, string cookieName,
StringBuilder cookieData, ref int size);

private static string RetrieveIECookiesForUrl(string url)
{
StringBuilder cookieHeader = new StringBuilder(new String(' ', 256), 256);
int datasize = cookieHeader.Length;
if (!InternetGetCookie(url, null, cookieHeader, ref datasize))
{
if (datasize < 0)
return String.Empty;
cookieHeader = new StringBuilder(datasize);
InternetGetCookie(url, null, cookieHeader, ref datasize);
}
return cookieHeader.ToString();
}


...全文
450 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cat_hsfz 2006-01-08
  • 打赏
  • 举报
回复
.NET Passport服务器发回来的Cookies是否会每次不同?
ttm1984 2006-01-08
  • 打赏
  • 举报
回复
同问!焦急中!。。。
ljq0316 2005-10-21
  • 打赏
  • 举报
回复
up
zju_mick 2005-08-08
  • 打赏
  • 举报
回复
up
zju_mick 2005-08-03
  • 打赏
  • 举报
回复
up
zju_mick 2005-08-02
  • 打赏
  • 举报
回复
我已经试过了将iehttpheader抓取的cookie手工添加到httpRequest,但发现服务器返回500内部错误,是不是服务器内部作了防模拟机制?
tigerwen01 2005-08-02
  • 打赏
  • 举报
回复
up
zju_mick 2005-08-02
  • 打赏
  • 举报
回复
up
zju_mick 2005-08-01
  • 打赏
  • 举报
回复
I will try iehttpheader.
zju_mick 2005-08-01
  • 打赏
  • 举报
回复
Thank rainlake!
I have tried "Request.AllowAutoRedirect = false", and this is the fetched html:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/bb/signin.aspx?rurl=http%3a%2f%2fgreenridge%2fbb%2
fpostlisting.aspx'>here</a>.</h2>
</body></html>
rainlake 2005-08-01
  • 打赏
  • 举报
回复
and more advice: Request.AllowAutoRedirect = false to test what happened

I can not input chinese .sorry
rainlake 2005-08-01
  • 打赏
  • 举报
回复
use iehttpheader ,you will find more helpful things
doubtpig 2005-08-01
  • 打赏
  • 举报
回复
同问

62,046

社区成员

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

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

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

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