网上普遍应用一个HttpRequest帖子模拟登录的然后又获取内容的如下:
http://www.cnblogs.com/dudu/articles/80713.html
以下是我想实现的:
string strLogin = Login("http://www.investoday.com.cn/login/index.jsp?errorcode=5", "Action='/login/submit.jsp'&USERID='tongliang1968'&Password='tongliang1968'") ; string strResult = getPage("http://www.investoday.com.cn/main/news.jsp?id=5", "Action=&data=") ;"Action=&data="  我不知道应该填写什么,总之就是想获取登录后的内容:http://www.investoday.com.cn/main/news.jsp?id=5
有可能有分页一起获取不知可否?问题:现在是获取不到页面内容,只能到要求登录的页面,郁闷!请教大家。十分感谢大家,希望大家能帮忙顶!

解决方案 »

  1.   

    要模拟登录.分析登录界面的html.看它送了哪些字段到服务器.
    你也要把相应的帐户,密码送到server.并把cookContainer对象保存起来,如果登录成功.
    下次还用这个cookContainer 去获取页面.
      

  2.   

    谢谢幕白,如下:
    if (HttpContext.Current.Application["cookieheader"] != null) 

    cookieheader = (string)HttpContext.Current.Application["cookieheader"]; 

    else 的时候才:string strLogin = Login("http://www.investoday.com.cn/login/index.jsp?errorcode=5", "Action='/login/submit.jsp'&USERID='tongliang1968'&Password='tongliang1968'")如下是两个函数:
    public static string Login(String url, String paramList) 

    HttpWebResponse res = null; 
    string strResult="";  try 
    {  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
    req.Method = "POST"; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    req.AllowAutoRedirect = false; 
    CookieContainer cookieCon = new CookieContainer(); 
    req.CookieContainer = cookieCon;  StringBuilder UrlEncoded = new StringBuilder(); 
    Char[] reserved = {'?', '=', '&'}; 
    byte[] SomeBytes = null;  if (paramList != null) 

    int i=0, j; 
    while(i<paramList.Length) 

    j=paramList.IndexOfAny(reserved, i); 
    if (j==-1) 

    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i))); 
    break; 

    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i))); 
    UrlEncoded.Append(paramList.Substring(j,1)); 
    i = j+1; 

    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString()); 
    req.ContentLength = SomeBytes.Length; 
    Stream newStream = req.GetRequestStream(); 
    newStream.Write(SomeBytes, 0, SomeBytes.Length); 
    newStream.Close(); 

    else 

    req.ContentLength = 0; 

    res = (HttpWebResponse)req.GetResponse(); 
    cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url)); 
    HttpContext.Current.Application.Lock(); 
    HttpContext.Current.Application["cookieheader"] = cookieheader; 
    HttpContext.Current.Application.UnLock();  Stream ReceiveStream = res.GetResponseStream(); 
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 
    StreamReader sr = new StreamReader( ReceiveStream, encode ); 
    Char[] read = new Char[256]; 
    int count = sr.Read( read, 0, 256 ); 
    while (count > 0) 

    String str = new String(read, 0, count); 
    strResult += str; 
    count = sr.Read(read, 0, 256); 


    catch(Exception e) 

    strResult = e.ToString(); 

    finally 

    if ( res != null ) 

    res.Close(); 

    }  return strResult; 

    public static string getPage(String url, String paramList) 

    HttpWebResponse res = null; 
    string strResult = "";  try 
    {  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
    req.Method = "POST"; 
    req.KeepAlive = true; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    CookieContainer cookieCon = new CookieContainer(); 
    req.CookieContainer = cookieCon; 
    req.CookieContainer.SetCookies(new Uri(url),cookieheader); 
    StringBuilder UrlEncoded = new StringBuilder(); 
    Char[] reserved = {'?', '=', '&'}; 
    byte[] SomeBytes = null;  if (paramList != null) 

    int i=0, j; 
    while(i<paramList.Length) 

    j=paramList.IndexOfAny(reserved, i); 
    if (j==-1) 

    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i))); 
    break; 

    UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i))); 
    UrlEncoded.Append(paramList.Substring(j,1)); 
    i = j+1; 

    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString()); 
    req.ContentLength = SomeBytes.Length; 
    Stream newStream = req.GetRequestStream(); 
    newStream.Write(SomeBytes, 0, SomeBytes.Length); 
    newStream.Close(); 

    else 

    req.ContentLength = 0; 

    res = (HttpWebResponse)req.GetResponse(); 
    Stream ReceiveStream = res.GetResponseStream(); 
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 
    StreamReader sr = new StreamReader( ReceiveStream, encode ); 
    Char[] read = new Char[256]; 
    int count = sr.Read( read, 0, 256 ); 
    while (count > 0) 

    String str = new String(read, 0, count); 
    strResult += str; 
    count = sr.Read(read, 0, 256); 


    catch(Exception e) 

    strResult = e.ToString(); 

    finally 

    if ( res != null ) 

    res.Close(); 

    }  return strResult; 

    请帮忙浏览。我也是摘录。
      

  3.   

    给你一点代码.
    StringBuilder sb = new StringBuilder();
    sb.Append("__EVENTTARGET=");
    sb.Append("&__EVENTARGUMENT=");
    sb.Append("&__VIEWSTATE="+ System.Web.HttpUtility.UrlEncode(sViewStateStirng));
    sb.Append("&CSDNUserLogin%3atb_UserName="+Uid);
    sb.Append("&CSDNUserLogin%3atb_Password="+Pwd);
    sb.Append("&CSDNUserLogin%3atb_ExPwd="+sImageCode);
    sb.Append("&CSDNUserLogin%3acb_SaveState="+(bSave?"on":""));
    sb.Append("&from=" +System.Web.HttpUtility.UrlEncode("地址"));
    sb.Append("&CSDNUserLogin%3aImage_Login.x=61");
    sb.Append("&CSDNUserLogin%3aImage_Login.y=7");


    System.Net.CookieContainer cook = Session.cook;
    string res = new DataCaptureObject().getHTMLByUrlCook("地址")
     
    if(res.IndexOf("成功登录")>-1)
    {
    Session.sUserCode = Uid;
    Session.bLogin = true;
    Session.cook = cook;
    if(bSave)
    new DataCaptureObject().SerializeObject(Session,Public.ConfigFolderPath +"cook.dll");
    return true;
    }
    else
    return false;




    //这是另一个类的
    public  string getHTMLByUrlCook(string url,ref System.Net.CookieContainer cook,string sMethod,string Param,bool bAutoRedirect)
    {
    sMethod = sMethod.ToUpper();
    sMethod = sMethod!="POST"?"GET":sMethod;
    string res ="";
    HttpWebRequest re =(HttpWebRequest)HttpWebRequest.Create(url);
    re.CookieContainer = cook; // attach the cook object 
    re.Method = sMethod;
    re.AllowAutoRedirect = bAutoRedirect;
    re.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)";


    if(Public.Session.bUsePox) //利用代理
    {
    int Port = 80;
    if(Public.IsInt(this.Session.PoxPort))
    {
    Port = Convert.ToInt32(this.Session.PoxPort);
    }
    WebProxy pox = new WebProxy(this.Session.PoxIp,Port);
    pox = (WebProxy)re.Proxy;
    }


     
    re.Referer = url;
    if (sMethod =="POST")  // Post data to Server
    {
    re.ContentType="application/x-www-form-urlencoded";
    Byte[] b = this.getByteFromString(Param);
    re.ContentLength = b.Length;
    try
    {
    Stream oSRe = re.GetRequestStream();
    oSRe.Write(b,0,b.Length);
    oSRe.Close();
    oSRe = null;
    }
    catch(Exception )
    {
    re = null;
    return "-1";
    }
    }

    HttpWebResponse rep = null;
    Stream oResponseStream = null;
    StreamReader oSReader = null;
    try
    {
    rep=(HttpWebResponse)re.GetResponse();
    oResponseStream = rep.GetResponseStream();
    oSReader  = new StreamReader(oResponseStream,System.Text.Encoding.Default);
    res =oSReader.ReadToEnd();
    }
    catch (System.Net.WebException  e)
    {
    //res ="-1";

    res = e.ToString();
    } if (rep!=null) 
    {
    rep.Close();
    rep = null;
    }
    if(oResponseStream!= null)
    {
    oResponseStream.Close();
    oResponseStream = null;
    }

    if(oSReader!=null)
    {
    oSReader.Close();
    oSReader = null;
    }
    re = null; return res;

    }
      

  4.   

    我看过只要登录上去就没问题,都能打开页面现在的问题是,下面写的正确么?:
    string strLogin = Login("http://www.investoday.com.cn/login/index.jsp?errorcode=5", "Action='/login/submit.jsp'&USERID='tongliang1968'&Password='tongliang1968'") ; 
    这个是否正确? Action='/login/submit.jsp' 
    string strResult = getPage("http://www.investoday.com.cn/main/news.jsp?id=5", "Action=&data=") ;
    处应该填写什么? "Action=&data="
      

  5.   

    cpp2017(幕白兄)  有直接可以用的代码么?
      

  6.   

    cpp2017(幕白兄)  有直接可以用的代码么
    ============我给你的代码就是我现在登录csdn的代码.
      

  7.   

    能模仿http://www.investoday.com.cn/login/index.jsp?errorcode=5给咱来个例子么?
    感激你一辈子。。
      

  8.   

    这个相当简单了
    提交这几个参数就可以了
    userid=用户名&issave=true&password=密码&issave1=true&Submit=登录
      

  9.   

    userid=用户名&issave=true&password=密码&issave1=true&Submit=登录 这几个参数在你的代码中的sb.Append("__EVENTTARGET=");
    sb.Append("&__EVENTARGUMENT=");
    sb.Append("&__VIEWSTATE="+ System.Web.HttpUtility.UrlEncode(sViewStateStirng));
    sb.Append("&CSDNUserLogin%3atb_UserName="+Uid);
    sb.Append("&CSDNUserLogin%3atb_Password="+Pwd);
    sb.Append("&CSDNUserLogin%3atb_ExPwd="+sImageCode);
    sb.Append("&CSDNUserLogin%3acb_SaveState="+(bSave?"on":""));
    sb.Append("&from=" +System.Web.HttpUtility.UrlEncode("地址"));
    sb.Append("&CSDNUserLogin%3aImage_Login.x=61");
    sb.Append("&CSDNUserLogin%3aImage_Login.y=7");
    应该如何写呢?
      

  10.   

    sb.Append("userid=用户名");
    sb.Append("&password=密码);
    ...........