代码语言
.
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
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
HttpOperater-模拟HTTP操作类
作者:
天坤
/ 发布于
2016/7/27
/
657
注:使用时,ref string cookiesOutput参数是服务器返回的Cookie,需保存用于下次请求。
using System; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; namespace Whir.Software.DocumentDownLoader.Library { /// <summary> /// 模拟HTTP操作 /// </summary> public class HttpOperater { /// <summary> /// 发起Http请求 /// </summary> /// <param name="httpRequestType">请求方式</param> /// <param name="url">请求地址</param> /// <param name="cookieInput">请求时传入的cookie</param> /// <param name="cookieOutput">服务器返回的cookie</param> /// <param name="postData">发送数据</param> /// <returns></returns> public static string DoRequest(HttpRequestType httpRequestType, string url, string cookieInput, ref string cookieOutput, string postData) { string response; try { const string windowsUserName = ""; const string windowsPwd = ""; const string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36"; const string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; const string acceptLanguage = "zh-CN,zh;q=0.8"; const string acceptEncoding = "gzip,deflate,sdch"; CookieContainer cookieContainer = GetCookie(url, cookieInput); var newUri = new Uri(url); var request = (HttpWebRequest)WebRequest.Create(newUri); request.PreAuthenticate = true; if (windowsUserName.Length > 0 & windowsPwd.Length > 0) { request.Credentials = new NetworkCredential(windowsUserName.Trim(), windowsPwd.Trim()); } request.Timeout = 20000; request.CookieContainer = cookieContainer; request.UserAgent = userAgent; request.Accept = accept; request.Headers["Accept-Language"] = acceptLanguage; request.Headers["Accept-Charset"] = acceptEncoding; request.Headers["Accept-Encoding"] = acceptEncoding; request.Referer = newUri.AbsoluteUri; request.Method = httpRequestType == HttpRequestType.GET ? "GET" : "POST"; if (request.Method == "POST") { request.ContentType = "application/x-www-form-urlencoded"; byte[] bytes = Encoding.UTF8.GetBytes(postData); request.ContentLength = bytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } } using (var wr = (HttpWebResponse)request.GetResponse()) { response = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd(); } CookieCollection cookies = cookieContainer.GetCookies(newUri); cookieOutput = CookieTostr(cookies); } catch (NotSupportedException exception) { response = exception.Message; } catch (InvalidOperationException exception) { response = exception.Message; } catch (IOException exception) { response = exception.Message; } catch (Exception exception) { response = exception.Message; } return response; } /// <summary> /// 设置cookie域 /// </summary> /// <param name="url">请求地址</param> /// <param name="cookieInput">cookie</param> /// <returns></returns> private static CookieContainer GetCookie(string url, string cookieInput) { var cookieContainer = new CookieContainer(); var cookies = new CookieCollection(); string[] cookiesArr = cookieInput.Split(';'); foreach (string s in cookiesArr) { string[] keyValuePair = s.Split('='); if (keyValuePair.Length > 1) { var cookie = new Cookie { Name = keyValuePair[0].Trim(), Value = keyValuePair[1].Trim(), Domain = GetDomain(url).Trim()//设置cookie域 }; cookies.Add(cookie); } } cookieContainer.Add(cookies); return cookieContainer; } /// <summary> /// 通过Url取得域 /// </summary> /// <param name="url"></param> /// <returns></returns> private static string GetDomain(string url) { var regex = new Regex("(?i)http[s]*://(?<domain>[\\w|.]*)", RegexOptions.CultureInvariant | RegexOptions.Compiled); return regex.Match(url).Groups["domain"].Value; } /// <summary> /// 将cookie转为字符串 /// </summary> /// <param name="cookies"></param> /// <returns></returns> private static string CookieTostr(CookieCollection cookies) { return cookies.Cast<Cookie>() .Aggregate(string.Empty, (current, c) => current + (c.Name + "=" + c.Value + ";")); } } /// <summary> /// HTTP请求方式 /// </summary> public enum HttpRequestType { /// <summary> /// GET /// </summary> GET = 1, /// <summary> /// POST /// </summary> POST = 2 } }
试试其它关键字
模拟HTTP操作类
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
天坤
贡献的其它代码
(
11
)
.
iframe自适应高度
.
根据某年某周获取一周的日期
.
判空相关
.
将txt文件内容(以utf-8)存储到数据库中
.
遍历list、set和map集合的方式
.
全局热键设置与窗体热键设置实例
.
HttpOperater-模拟HTTP操作类
.
显示或是删除input域中的默认值
.
Http相关辅助类 HttpUtils
.
最小树形图(邻接阵形式)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3