[asp.net]利用HttpRequest登录到某个网站,然后获取网站信息

news/2024/5/10 12:42:34/文章来源:https://blog.csdn.net/aigubi2836/article/details/102267916
问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。

解决:使用asp.net中的httprequest和httpresponse来实现。

要点:
1。 通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代表SESSION ID的COOKIE。 见Login方法
2。 将此COOKIE包含在一个cookiecontainer中并附加到另一个HTTPREQUEST请求中,则可以实现SESSION的还原。见getPage方法

源程序如下:
getHttpInfo.aspx:
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="getHttpInfo.aspx.cs" AutoEventWireup="false" Inherits="PdfTest.getHttpInfo"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif        
< meta  content ="Microsoft Visual Studio 7.0"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
getHttpInfo.aspx.cs:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
// using System.Data.OleDb;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Net;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  Microsoft.Data.Odbc;
None.gif
None.gif
namespace  PdfTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class getHttpInfo : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected static string cookieheader;
InBlock.gif
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Put user code to initialize the page here
InBlock.gif

InBlock.gif            
string strResult;
InBlock.gif
InBlock.gif            
if (HttpContext.Current.Application["cookieheader"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cookieheader 
= (string)HttpContext.Current.Application["cookieheader"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Login into the website and keep the cookie for the session in the application variable
InBlock.gif
                string strLogin = Login("http://www.thesiteyouwanttovisit/theloginpage.asp""Action=&USERID=&Password=") ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif
InBlock.gif            strResult 
= getPage("http://www.thesiteyouwanttovisit/theloginpage.asp""Action=&data=") ;
InBlock.gif
InBlock.gif
InBlock.gif            
//Write the result to htm file
InBlock.gif
            FileStream htmFile = new FileStream("c:save.htm", FileMode.OpenOrCreate);
InBlock.gif            StreamWriter sw 
= new StreamWriter(htmFile);
InBlock.gif            sw.Write(strResult);
InBlock.gif            sw.Close();
InBlock.gif            htmFile.Close();
InBlock.gif
InBlock.gif            
// output the result
InBlock.gif
            Response.Write(strResult);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static string Login(String url, String paramList) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpWebResponse res 
= null;
InBlock.gif            
string strResult="";
InBlock.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                HttpWebRequest req 
= (HttpWebRequest)WebRequest.Create(url);
InBlock.gif                req.Method 
= "POST";
InBlock.gif                req.ContentType 
= "application/x-www-form-urlencoded";
InBlock.gif                req.AllowAutoRedirect 
= false;
InBlock.gif                CookieContainer cookieCon 
= new CookieContainer();
InBlock.gif                req.CookieContainer 
= cookieCon;
InBlock.gif
InBlock.gif                StringBuilder UrlEncoded 
= new StringBuilder();
ExpandedSubBlockStart.gifContractedSubBlock.gif                Char[] reserved 
= dot.gif{'?''=''&'};
InBlock.gif                
byte[] SomeBytes = null;
InBlock.gif
InBlock.gif                
if (paramList != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int i=0, j;
InBlock.gif                    
while(i<paramList.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        j
=paramList.IndexOfAny(reserved, i);
InBlock.gif                        
if (j==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length
-i)));
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

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

InBlock.gif                    SomeBytes 
= Encoding.UTF8.GetBytes(UrlEncoded.ToString());
InBlock.gif                    req.ContentLength 
= SomeBytes.Length;
InBlock.gif                    Stream newStream 
= req.GetRequestStream();
InBlock.gif                    newStream.Write(SomeBytes, 
0, SomeBytes.Length);
InBlock.gif                    newStream.Close();
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    req.ContentLength 
= 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif
InBlock.gif                res 
= (HttpWebResponse)req.GetResponse();
InBlock.gif                cookieheader 
= req.CookieContainer.GetCookieHeader(new Uri(url));
InBlock.gif                HttpContext.Current.Application.Lock();
InBlock.gif                HttpContext.Current.Application[
"cookieheader"= cookieheader;
InBlock.gif                HttpContext.Current.Application.UnLock();
InBlock.gif
InBlock.gif                Stream ReceiveStream 
= res.GetResponseStream();
InBlock.gif                Encoding encode 
= System.Text.Encoding.GetEncoding("utf-8");
InBlock.gif                StreamReader sr 
= new StreamReader( ReceiveStream, encode );
InBlock.gif                Char[] read 
= new Char[256];
InBlock.gif                
int count = sr.Read( read, 0256 );
InBlock.gif                
while (count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    String str 
= new String(read, 0, count);
InBlock.gif                    strResult 
+= str;
InBlock.gif                    count 
= sr.Read(read, 0256);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strResult 
= e.ToString();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( res != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    res.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return strResult;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static string getPage(String url, String paramList) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpWebResponse res 
= null;
InBlock.gif            
string strResult = "";
InBlock.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                HttpWebRequest req 
= (HttpWebRequest)WebRequest.Create(url);
InBlock.gif                req.Method 
= "POST";
InBlock.gif                req.KeepAlive 
= true;
InBlock.gif                req.ContentType 
= "application/x-www-form-urlencoded";
InBlock.gif                CookieContainer cookieCon 
= new CookieContainer();
InBlock.gif                req.CookieContainer 
= cookieCon;
InBlock.gif                req.CookieContainer.SetCookies(
new Uri(url),cookieheader);
InBlock.gif                StringBuilder UrlEncoded 
= new StringBuilder();
ExpandedSubBlockStart.gifContractedSubBlock.gif                Char[] reserved 
= dot.gif{'?''=''&'};
InBlock.gif                
byte[] SomeBytes = null;
InBlock.gif
InBlock.gif                
if (paramList != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int i=0, j;
InBlock.gif                    
while(i<paramList.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        j
=paramList.IndexOfAny(reserved, i);
InBlock.gif                        
if (j==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length
-i)));
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

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

InBlock.gif                    SomeBytes 
= Encoding.UTF8.GetBytes(UrlEncoded.ToString());
InBlock.gif                    req.ContentLength 
= SomeBytes.Length;
InBlock.gif                    Stream newStream 
= req.GetRequestStream();
InBlock.gif                    newStream.Write(SomeBytes, 
0, SomeBytes.Length);
InBlock.gif                    newStream.Close();
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    req.ContentLength 
= 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif
InBlock.gif                res 
= (HttpWebResponse)req.GetResponse();
InBlock.gif                Stream ReceiveStream 
= res.GetResponseStream();
InBlock.gif                Encoding encode 
= System.Text.Encoding.GetEncoding("utf-8");
InBlock.gif                StreamReader sr 
= new StreamReader( ReceiveStream, encode );
InBlock.gif                Char[] read 
= new Char[256];
InBlock.gif                
int count = sr.Read( read, 0256 );
InBlock.gif                
while (count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    String str 
= new String(read, 0, count);
InBlock.gif                    strResult 
+= str;
InBlock.gif                    count 
= sr.Read(read, 0256);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strResult 
= e.ToString();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( res != null ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    res.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return strResult;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/cai9911/archive/2006/10/17/531137.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_900202.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

3个CSS动画库,比Animated还好用,让你的网站酷炫起来

本文首发于https://www.1024nav.com/tools/css-animation-library 转载请注明出处 整理了日常前端开发中常用的css动画库&#xff0c;让你的网页动起来&#xff0c;可以在生成中使用&#xff0c;非常酷炫&#xff0c;值得收藏 animxyz AnimXYZ 可帮助您为网站创建、自定义和…

2022年前端面试必备网站【1024nav】它来了~

整理了大半年的前端面经&#xff0c;终于上线了&#xff5e;&#xff5e;欢迎收藏&#xff0c;关注&#xff0c;转发。 1024nav是什么? 1024nav是一个前端开发者必备的学习网站&#xff0c;网站的内容都是精心编写的原创内容。 为什么做这个网站呢&#xff1f; 主要是现在很…

让网站变成灰白色(网站去色)

在html标签添加filter属性 html {filter: grayscale(100%);}添加的效果如下&#xff1a; 去除效果

使用Tale搭建个人博客网站(基于java)

前言&#xff08;背景&#xff09;&#xff1a; 最近几天笔者在搭建一个博客网站。由于作者有自己的服务器&#xff0c;本着不浪费资源的态度&#xff0c;于是就在其上部署个博客网站。虽然使用学生优惠申请的服务器&#xff08;最低配&#xff09;&#xff0c;但用来更新下自己…

腾讯云Ubuntu安装JDK、tomcat、mysql、部署网站步骤详解

腾讯云Ubuntu配置部署步骤&#xff1a;安装JDK、Tomcat&#xff0c;mysql。部署一个Web的项目演示 1.购买服务器&#xff1a;这个不用我说了&#xff0c;学生党可以直接用自己的优惠一元购机 2.Xshell和Xftp或者winscp 因为是远程操作服务器&#xff0c;用一个命令操作页面最好…

程序员必备精粹网站汇总

文章目录 &#xff11;、超好用的搜索引擎(1)秘迹搜索(2)快搜&#xff0d;快人一步(3) DogeDoge搜索(4)GitLogs(5)小白盘 2、学习网站(1)ACM编程学习网站(2)面试刷题学习网站(3)基础语言学习 3、ppt4、图片操作(1) 图片无限变大(2) waifu2x无损放大图片(3) Remove Image Backgr…

前端笔记—从入门到坟墓[网站基础与优化][12.1]

网站icon图标 引入方式&#xff1a; <link rel"shortcut icon" href"favicon.ico" type"image/x-icon">ico图片制作方式&#xff1a; 1&#xff0c;准备一张400*400的图片。 2&#xff0c;登陆http://www.bitbug.net/网站进行图片格式转…

25 个精美的手机网站模板

现如今&#xff0c;iPhone 横行&#xff0c;Android 满天飞&#xff0c;WP7 蓄势待发的势态下&#xff0c;网站没有个手机版都好像很 out 一般。 于是本文向你推荐 25 个制作精美的手机网站模板&#xff0c;提供模板下载以及在线演示&#xff0c;你值得拥有。 Dossier Mobi Sti…

网站内容结构化探讨[1]

无意间浏览到一个承接网页制作的公司的网站——唐宋中国。   首先网站的设计给我一个很好的印象&#xff0c;因此特意留意了一下它的源代码&#xff08;做页面的职业病&#xff09;&#xff0c;结果发现了一些问题&#xff0c;而这些问题&#xff0c;可能正好是学习web标准的…

网站内容结构化探讨[2]--主导航的问题

结构化确实是个让人头疼的工作。为了追求一个好的结构&#xff08;或者说比较好的&#xff09;&#xff0c;往往需要花费掉很多时间&#xff0c;这在追求效率的今天&#xff0c;也许是不被老板允许的。所以很多网站虽然看上去抛弃了表格布局&#xff0c;而其实质上&#xff0c;…

知识产权相关网站2021.7更新

#知识产权相关网站 网站一&#xff1a;WIPO[Directory of Intellectual Property Offices] https://www.wipo.int/directory/en/urls.jsp 网站二&#xff1a;国家知识产权 - 公共服务网 http://ggfw.cnipa.gov.cn:8010/PatentCMS_Center/ 网站地图-IPR http://ipr.mofco…

厚积薄发2021最新个人站长建站模版发布震惊【qipinfuwu.com.V1.0】

厚积薄发2021最新个人站长建站程序发布震惊【qipinfuwu.com.V1.0】实例可直接运行 1.0文件清单 建站程序及文件 1-主页模块.bat 代码 echo off for /f %%i in (‘dir /ad /b’) do copy zhuye.html %%i for /f %%i in (‘dir /ad /b’) do copy 4-页面清单.bat %%i for …

Java、JSP校园信息交流发布网站设计与实现

技术&#xff1a;Java、JSP等 摘要&#xff1a;在高新技术发展的今天&#xff0c;因特网的高速发展给人们带来了极大的便利&#xff0c;使人们的生活变得更加的丰富多彩&#xff0c;人们在生活中通过网络交流获得更多的信息。特别是年轻的大学生群体中更是对周围的信息交流有着…

Java、JSP校友录管理网站的设计与实现

技术&#xff1a;Java、JSP等 摘要&#xff1a;随着B/S模式越来越受到人们的接受&#xff0c;各种在浏览器中给我们带来的服务应运而生&#xff0c;更多的用户花费更多的时间在这些系统中&#xff0c;在随着潮流的同时&#xff0c;我便想利用这次毕业设计的时间来做一个对广大学…

利用QQ通讯组件实现网站立刻联系客服以及一键加群的功能【C#winform实现】

创建一个windows窗体&#xff0c;添加 .cs代码&#xff1a; private void simpleButton1_Click(object sender, EventArgs e){Process.Start("CHROME.EXE", "http://wpa.qq.com/msgrd?v3&uin联系人的qq号&siteqq&menuyes");/*<a target&qu…

两款 Js 插件为你的网站添彩

在网页中合理的利用一些特效能带给人眼前一亮的感觉。今天给大家分享两款很有意思的 Js 特效插件。 输入框打字冒光特效 这款特效本博客也在使用&#xff0c;也有很多人问过是怎么实现的。具体的效果请看 GIF 图&#xff1a; 食用方法&#xff1a; 普通网站&#xff1a; 在网站…

从ST网站下载STM32标准库的流程

打开搜索器&#xff0c;搜索ST官方网站&#xff0c;打开。 如果英文不是很好&#xff0c;点击更换中文。 找到STM32微控制软件&#xff0c;点击打开。 找到标准外设软件库 点击我们想要的系列&#xff0c;示例以F4为基准。 点击获取软件。 点击下载 STM32F4

自学网页开发,打算自己开发一个简单的网站,记录下自己开发过程

本人去年转行软件行业&#xff0c;在深圳一家外包公司工作&#xff0c;学的东西跟自己想做的JAVA WEB开发完全不同&#xff0c;只能说是了解了下软件行业的一点门路吧&#xff0c;为了坚持自己的路&#xff0c;打算自己开发一个简单的网站&#xff0c;并记录下自己开发过程&…

我个人制作的网页网站作品,自学网站制作,自学网页制作网页制作视频教程

要想学做网页&#xff0c;首先得了解制作网页的工具Dreamweaver&#xff1a;这是网页三剑客之一&#xff0c;专门制作网页的工具&#xff0c;可以自动将网页生成代码&#xff0c;是普通网页制作者的首选工具&#xff0c;界面简单&#xff0c;实用功能比较强大。建议初学者选用。…

vue开发旅行网站教程(1):项目开发流程及环境搭建

第一章&#xff1a;项目起步 一、整体开发流程 二、开发所需环境 第二章&#xff1a;环境搭建 第一步&#xff1a;nodejs及npm的安装 下载地址&#xff1a;node官网下载地址&#xff0c;下载相应的安装包&#xff0c;安装即可。 安装完毕后&#xff0c;winr&#xff0c;输入…