http://onepoint.infor.com之前某个朋友写的自动登陆代码如下,但是不知道如何登陆这个网站。
//登陆时调用
//login("http://thewebsite/", "username=yourname&password=yourpwd") ;
private string login(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";
string cookieheader = "";
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));
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
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;
} //获取数据时调用
private void getPage(String url, String paramList)
{
HttpWebResponse res = null;
string cookieheader = "";
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();
byte[] buffer = new byte[1024];
string filename = "c:\\temp\\temp.data";
Stream outStream = File.Create(filename);
int l;
do
{
l = ReceiveStream.Read(buffer, 0,
buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
} while (l > 0); outStream.Close(); }
catch (Exception)
{
}
finally
{
if (res != null)
{
res.Close();
}
}
return;
}
就类似网页抓取.只不过要先登陆.
用不着什么iehttpheaders了吧,我觉得楼主只要把第一个子程序改一改就行了,就是那个private string login(String url, String paramList){}中返回值最终改成cookieheader,也就是cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url))那一行的结果,而不是Stream ReceiveStream = res.GetResponseStream()的字符串结果strResult += str,然后,把第一个子程序的返回值作为参数传给第二个子程序private void getPage(String url, String paramList){}(参数应该再加一个,用来接受cookieheader的值),因为我看了一下,在第二个子程序里,楼主虽然定义了一个名为string cookieheader = ""的变量,但一直没有对他传值,所以cookieheader的值一直都是空,所以也就得不到验证,无法访问请求的页面内容。
我用httpwatch观测到post的东西是:
<parameter name="__VIEWSTATE">dDwxMzk4Njg0NTM5O3Q8O2w8aTwzPjs+O2w8dDw7bDxpPDE+O2k8Mz47aTw3Pjs+O2w8dDxwPHA8bDxUZXh0Oz47bDxJbmZvciBPbmVQb2ludCBPbmxpbmU7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPFBsZWFzZSBlbnRlciB5b3VyIGVtYWlsIGFkZHJlc3MgYW5kIHBhc3N3b3JkIGJlbG93Ljs+Pjs+Ozs+O3Q8dDxwPHA8bDxWaXNpYmxlOz47bDxvPGY+Oz4+Oz47Oz47Oz47Pj47Pj47PmH66Tpp6vVvy2W0zDVGa+ckfXPG</parameter>
<parameter name="txtEmail">[email protected]</parameter>
<parameter name="txtPassword">XXXXXX</parameter>
<parameter name="cmdLogin">Verify My Account Information</parameter> 那么在程序中,是否需要把__VIEWSTATE的内容Post过去?