大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 网络安全 -> 网络其他 -> 利用HttpRequest

利用HttpRequest

时间: 2021-07-31 作者:daque

利用HttpRequest利用HttpRequest登录到某个网站,而后获得网站消息的步调示例 题目:有的网站的关系实质必需要在登录后才不妨察看,其登录消息生存在session变量之中。如许,运用asphttp等组件就难以精确获得所要的消息。处置:运用asp.net中的httprequest和httpresponse来实行。重心:1。 经过附加一个cookiecontainer到httprequest东西中,不妨获得登录后归来的代办session id的cookie。 见login本领2。 将此cookie包括在一个cookiecontainer中并附加到另一个httprequest乞求中,则不妨实行session的恢复。见getpage本领源步调如次:gethttpinfo.aspx:<%@ page language="c#" codebehind="gethttpinfo.aspx.cs" autoeventwireup="false" inherits="pdftest.gethttpinfo" %><!doctype html public "-//w3c//dtd html 4.0 transitional//en" ><html><head><title>webform1</title><meta content="microsoft visual studio 7.0" name="generator"><meta content="c#" name="code_language"><meta content="javascript" name="vs_defaultclientscript"><meta content="http://schemas.microsoft.com/intellisense/ie5"; name="vs_targetschema"></head><body><form id="form1" method="post" runat="server"></form></body></html>gethttpinfo.aspx.cs:using system;using system.collections;using system.componentmodel;using system.data;//using system.data.oledb;using system.drawing;using system.web;using system.web.sessionstate;using system.web.ui;using system.web.ui.webcontrols;using system.web.ui.htmlcontrols;using system.net;using system.io;using system.text;using system.text.regularexpressions;using microsoft.data.odbc;namespace pdftest{/// <summary>/// summary description for webform1./// </summary>public class gethttpinfo : system.web.ui.page{protected static string cookieheader;private void page_load(object sender, system.eventargs e){// put user code to initialize the page herestring strresult;if (httpcontext.current.application["cookieheader"] != null){cookieheader = (string)httpcontext.current.application["cookieheader"];}else{//login into the website and keep the cookie for the session in the application variablestring strlogin = login("http://www.thesiteyouwanttovisit/theloginpage.asp";, "action=&userid=&password=") ;}strresult = getpage("http://www.thesiteyouwanttovisit/theloginpage.asp";, "action=&data=") ;//write the result to htm filefilestream htmfile = new filestream("c:\save.htm", filemode.openorcreate);streamwriter sw = new streamwriter(htmfile);sw.write(strresult);sw.close();htmfile.close();// output the resultresponse.write(strresult);}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;}#region web form designer generated codeoverride protected void oninit(eventargs e){//// codegen: this call is required by the asp.net web form designer.//initializecomponent();base.oninit(e);}/// <summary>/// required method for designer support - do not modify/// the contents of this method with the code editor./// </summary>private void initializecomponent(){ this.load += new system.eventhandler(this.page_load);}#endregion}}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.