网上普遍应用一个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
有可能有分页一起获取不知可否?问题:现在是获取不到页面内容,只能到要求登录的页面,郁闷!请教大家。十分感谢大家,希望大家能帮忙顶!
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
有可能有分页一起获取不知可否?问题:现在是获取不到页面内容,只能到要求登录的页面,郁闷!请教大家。十分感谢大家,希望大家能帮忙顶!
解决方案 »
- 懂JS和ajax的高手帮忙指点一下,快疯了!
- ASP.NET回传时如何保存FCK里的值?
- 如何肢解该由“StringBuilder”拼接起来的“SQL语句”???
- Linq命名空间之间关系问题?
- asp.net 如何编辑GIF动画?或是生成一个GIF动画?
- !!!!!!DG合并行!!!!!!在线等解决办法
- JavaScript问题和.NET问题...
- 我想用一个论坛来保存我的资料,大家推荐下。
- 请问怎么把一个网页的信息传到加一个网页上
- 如何设置一个DATAGRID的每条记录行的高度?属性生成器里只有列的设置(包括列的字体大小)?
- 将数据绑定在datagrid或者datalist中,怎样才能保持原来的格式?
- 请教有DRM经验的人
你也要把相应的帐户,密码送到server.并把cookContainer对象保存起来,如果登录成功.
下次还用这个cookContainer 去获取页面.
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;
}
请帮忙浏览。我也是摘录。
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;
}
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="
============我给你的代码就是我现在登录csdn的代码.
感激你一辈子。。
提交这几个参数就可以了
userid=用户名&issave=true&password=密码&issave1=true&Submit=登录
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");
应该如何写呢?
sb.Append("&password=密码);
...........