关于用HttpWebRequest登录网页

wolf516 2006-01-05 11:08:22
1)我已经的到Cookie的文本串:
cdb_sid=5bDuqi; cdb_cookietime=315360000; cdb_visitedfid=5D4D3; cdb_auth=B1MGBgxUXApeAVUADQMEUVdXD1UFVQZWAAUGBVQBWAE8b1E; cdb_fid5=1136348976

如何通过HttpWebRequest打开需要登录的网页,得到里面的代码?
Dim cc As New CookieContainer
Dim c As New Cookie
tCook = AxWebBrowser1.Document.cookie'取浏览器的cookie
V = Split(tCook, ";")
For i = 0 To UBound(V)
Temp = Trim(V(i) & "")
If Trim(Temp & "") <> "" Then
c.Name = Temp.Substring(0, InStr(Temp, "=") - 1)
c.Value = Temp.Substring(InStr(Temp, "="))
cc.Add(uri, c)
End If
Next
httpReq.CookieContainer = cc

再用HttpResponse打开网页时,得到的还是没有登录的状态.
小弟初接触vb.net及它的http编程.希望哪位高手给指教一下.
...全文
215 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fffgtao 2006-02-28
  • 打赏
  • 举报
回复
Qqwwee_Com 2006-02-15
  • 打赏
  • 举报
回复

请参考我尾巴后面的软件
有源代码的。



====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2

qugui 2006-02-15
  • 打赏
  • 举报
回复
public static string Login(String url, String paramList,string referer)
{
//url form 里面的action地址
//paramList 要post过去的参数 确定按钮的值也要传过去
//转移地址
HttpWebResponse res = null;
paramList=paramList;
string strResult="";

try
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.KeepAlive=true;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.Referer=referer;
req.Accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
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();
string [] key = res.Headers.AllKeys;
foreach(string l in key)
{
strResult+=l+":"+res.Headers[l]+"\r\n";
}
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;
}

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧