我用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();
        }

解决方案 »

  1.   

    use iehttpheader ,you will find more helpful things
      

  2.   

    and more advice: Request.AllowAutoRedirect = false to test what happenedI can not input chinese .sorry
      

  3.   

    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>
      

  4.   

    I will try iehttpheader.
      

  5.   

    我已经试过了将iehttpheader抓取的cookie手工添加到httpRequest,但发现服务器返回500内部错误,是不是服务器内部作了防模拟机制?
      

  6.   

    .NET Passport服务器发回来的Cookies是否会每次不同?