代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
模拟访问的网页源代码
作者:
Malcolm
/ 发布于
2015/4/16
/
900
/// <summary> /// 常用的Http工具函数集合 /// </summary> class HtmlTool { /// <summary> /// 模拟POST访问 /// </summary> /// <param name="postUrl">模拟POST的URL</param> /// <param name="postDataStr">需要POST的数据</param> /// <returns>模拟访问的网页源代码</returns> public static string sendPost(string postUrl, string postDataStr) { string retString = ""; if (Global.noException) { //用来存放cookie CookieContainer cookie = null; HttpWebRequest request = null; Stream myRequestStream = null; StreamWriter myStreamWriter = null; HttpWebResponse response = null; Stream myResponseStream = null; StreamReader myStreamReader = null; try { cookie = new CookieContainer(); //发送一个POST请求 request = (HttpWebRequest)WebRequest.Create(postUrl); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); request.CookieContainer = cookie; myRequestStream = request.GetRequestStream(); myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(postDataStr); //获取返回的内容 response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri); myResponseStream = response.GetResponseStream(); myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); } catch (Exception ex) { Global.noException = false; Console.Write("发送POST失败!\n报错:" + ex.Message); } finally { if (myStreamWriter != null) { myStreamWriter.Close(); } if (myStreamReader != null) { myStreamReader.Close(); } if (myResponseStream != null) { myResponseStream.Close(); } if (response != null) { response.Close(); } } } return retString; } /// <summary> /// 模拟GET访问 /// </summary> /// <param name="getUrl">模拟GET的URL</param> /// <returns>模拟访问的网页源代码</returns> public static string SendGet(string getUrl) { string retString = ""; if (Global.noException) { HttpWebRequest request = null; HttpWebResponse response = null; Stream myResponseStream = null; StreamReader myStreamReader = null; try { request = (HttpWebRequest)WebRequest.Create(getUrl); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; response = (HttpWebResponse)request.GetResponse(); myResponseStream = response.GetResponseStream(); myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); } catch (Exception ex) { Global.noException = false; Console.Write("发送GET失败!\n报错:" + ex.Message); } finally { if (myStreamReader != null) { myStreamReader.Close(); } if (myResponseStream != null) { myResponseStream.Close(); } if (response != null) { response.Close(); } } } return retString; } /// <summary> /// 获取某个URL的重定向地址 /// </summary> /// <param name="getUrl">原始的地址</param> /// <returns>重定向之后的地址</returns> public static string GetRedirection(string getUrl) { if (Global.noException) { HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)WebRequest.Create(getUrl); request.AllowAutoRedirect = false; request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; response = (HttpWebResponse)request.GetResponse(); if (response.Headers.Get("Location") == null) return ""; return response.Headers.Get("Location"); } catch (Exception ex) { Global.noException = false; Console.Write("获取重定向失败!报错:\n" + ex.Message); } finally { if (response != null) { response.Close(); } } } return ""; } /* * 在post的时候有时也用的到cookie * 所以在外部一个cookie属性随时保存 * CookieContainer cookie = new CookieContainer(); * 注意:有时候请求会重定向,但我们就需要从重定向url获取东西,可以用: * request.AllowAutoRedirect = false; * 设置重定向禁用,你就可以从headers的Location属性中获取重定向地址 */ }
试试其它关键字
模拟POST访问
POST
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Malcolm
贡献的其它代码
(
15
)
.
导出页面内容至word文档
.
解压缩文件
.
获取图片文件的宽度和高度
.
创建Excel并写入内容
.
Json转换到字典
.
基姆拉尔森计算公式计算日期
.
子窗体在父窗体指定的控件中显示
.
DevExpress.XtraTreeList.TreeList 显示行号
.
模拟访问的网页源代码
.
检查是否联网
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3