我现在是通过登录信息访问网页。(附原码)
我想直接通过帐号和密码访问,请指教。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;namespace GetUrl
{
    public class GetHtmlUrl
    {
        private static string CookVlaue = FileOpenCookies();                /// <summary>
        /// 根据网址返回对应HTML代码
        /// </summary>
        /// <param name="UriAddress"></param>
        /// <returns></returns>
        public static string GetUrlTxt(string UriAddress)
        {
            HttpWebRequest HttpWreq = (HttpWebRequest)WebRequest.Create(UriAddress);  //初始化打开网页实例
            //HttpWreq.ContentType = "application/x-www-form-urlencoded";
            //HttpWreq.ContentType = "application/x-shockwave-flash";  
            Cookie mycookie = new Cookie();
            mycookie.Name = "T3E";
            if (CookVlaue == " ")
            {
                return "没有读取到用户登陆信息";
            }            
            mycookie.Value = CookVlaue;
            mycookie.Domain = "s2.travian.cn";
            //给mycookie值 
            CookieContainer mycookiecontainer = new CookieContainer();            
            mycookiecontainer.Add(mycookie);
            
            HttpWreq.CookieContainer = mycookiecontainer;            HttpWreq.Method = "GET";
            HttpWreq.AllowAutoRedirect = true;
            HttpWreq.KeepAlive = true;
            HttpWebResponse HttpWresp = (HttpWebResponse)HttpWreq.GetResponse();                        //Console.WriteLine(HttpWresp.ProtocolVersion.ToString());  
            //返回版本号
            Encoding gb2312 = Encoding.GetEncoding("GB2312");
            Encoding Utf8 = Encoding.GetEncoding("UTF-8");
            Stream myStream = HttpWresp.GetResponseStream();
            StreamReader myReader = new StreamReader(myStream, Utf8);
            string var = myReader.ReadToEnd();            myReader.Close(); //关闭
            HttpWresp.Close();
            
            return var;  //返回取回的值
        }        /// <summary>
        /// 读取用户登陆信息,如果成功返回登陆信息,否则返回 "没有读取到用户登陆信息"
        /// </summary>
        /// <returns></returns>
        private static string FileOpenCookies()
        {
            string RetCookValue = "";   //存放读出来的COOKIE值
            try
            {
                string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
                string username = Environment.UserName;
                CookiePath += "\\" + username + "@s2.travian[2].txt";
                if (!File.Exists(CookiePath))
                {
                    CookiePath = CookiePath.Replace("[2]", "[1]");
                }
                string[] cookieValue = File.ReadAllLines(CookiePath, Encoding.ASCII);
                if (cookieValue.Length == 9)
                {
                    RetCookValue = cookieValue[1];                    
                    RetCookValue = Uri.UnescapeDataString(RetCookValue);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return " ";
            }
            return RetCookValue;
        }
    }
}

解决方案 »

  1.   

    用sniffer软件截获ie访问的数据,和你的比较一下
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  2.   

    以前用WebBrowser做过模拟登录,HttpRequest还没试过
      

  3.   

    现在我不知道用什么属性去填充帐号和密码。
    我现在的程序是可以访问相关的登录信息,但是都是ie的登录信息(ie自动生成的信息文件),我想直接用帐号和密码登录。谁知道怎么登录?
      

  4.   

     string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies); 
                    string username = Environment.UserName; 
    这儿能得到预期值吗?
    如果不能,请参考:http://blog.csdn.net/longren629/archive/2008/11/28/3400351.aspx
      

  5.   

    string username = Environment.UserName; 
    这个关键,看能否得到本地用户名
    否则像我一样用activex顺便帮忙看看,我遇到的问题。
    http://topic.csdn.net/u/20081231/12/3d0afdc3-8920-4347-80de-24d290f347eb.html 
      

  6.   

    string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies); 
    可以获取到预定的登录信息。我现在是通过这些登录信息来访问网站。
    我想改为帐号和密码访问,请指教。
      

  7.   

    加一个pagebase文件,每个页都继承一下,然后在pagebase 处理你的帐号和密码,存在cookie和session中, 就可以了啊
      

  8.   

    直接用form认证不就得了?有这么简单的东西不用,搞那么复杂
      

  9.   

    HttpWreq.Method = "GET"; get方式是直接请求url的,不能传递post参数
    你应该用post方式请求 即:
    HttpWreq.Method = "POST"; 
    然后加上post参数:
    至于代码我就不贴了
    具体方法你可以在百度里搜一下 httpwebrequest post 会出来一堆代码的。
      

  10.   

    直接 Request[“username”] 就可以了
      

  11.   

    .NET基础知识
    你可以去看一下这个网站里关于ASP.NET资料学习
    http://www.singletowm.com
      

  12.   

    我现在是通过cookies里面的信息连接的网站。我想用帐号和密码连接网站,该怎么做。
    最好能够提供原码..谢谢
      

  13.   

    不行啊
    我访问的量太大了。
    最高会到40万次左右,如果使用fiddler的话,速度能跟上吗?