.net后台模拟浏览器get/post请求
#region 后台模拟浏览器get/post请求
/// <summary>
/// 发送请求方式
/// </summary>
/// <param name="url">请求Url</param>
/// <param name="para">请求参数</param>
/// <param name="method">请求方式GET/POST</param>
/// <returns></returns>
public static string SendRequest(string url, string para, string method)
{
string strResult = "";
if (url == null || url == "")
return null;
if (method == null || method == "")
method = "GET";
// GET方式
if (method.ToUpper() == "GET")
{
try
{
System.Net.WebRequest wrq = System.Net.WebRequest.Create(url + para);
wrq.Method = "GET";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
System.Net.WebResponse wrp = wrq.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
strResult = sr.ReadToEnd();
}
catch (Exception ex)
{
return ex.Message;
}
}
// POST方式
if (method.ToUpper() == "POST")
{
&& para.IndexOf()
{
para = para.Substring();
}
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (para != null)
{
, j;
while (i < para.Length)
{
j = para.IndexOfAny(reserved, i);
)
{
UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.GetEncoding("utf-8")));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.GetEncoding("utf-8")));
UrlEncoded.Append(para.Substring(j, ));
i = j + ;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, , SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = ;
}
try
{
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Byte[] read = ];
, );
)
{// 如果内容以 ANSI 代码页形式
// Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
strResult += encode.GetString(read, , bytes);
bytes = ReceiveStream.Read(read, , );
}
return strResult;
}
catch (Exception ex)
{
return ex.Message;
}
}
return strResult;
}
#endregion
public static string SendRequest(string url, string para)
{
return SendRequest(url, para, "GET");
}
.net后台模拟浏览器get/post请求的更多相关文章
- http请求及模拟浏览器发送http请求
/** *测试新增图片 * @throws IOException * @throws HttpException * @throws SAXException * @th ...
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- curl模拟浏览器进行phpQuery抓取数据
报Warning: file_get_contents(http://www.dianping.com/shop/8042874) [function.file-get-contents]: fail ...
- 浏览器助手,请求拦截,后台模拟键鼠操作,页内嵌入JS
http://www.cnblogs.com/ /// <summary> /// 网页浏览器助手 /// 请求拦截,后台模拟键鼠操作,页内嵌入JS ...
- 基于nodejs模拟浏览器post请求爬取json数据
今天想爬取某网站的后台传来的数据,中间遇到了很多阻碍,花了2个小时才请求到数据,所以我在此总结了一些经验. 首先,放上我所爬取的请求地址http://api.chuchujie.com/api/?v= ...
- httpClient模拟浏览器发请求
一.介绍 httpClient是Apache公司的一个子项目, 用来提高高效的.最新的.功能丰富的支持http协议的客户端编程工具包.完成可以模拟浏览器发起请求行为. 二.简单使用例子 : 模拟浏览器 ...
- 第五十九节,模拟浏览器请求Python结合html基本格式
模拟浏览器请求Python结合html基本格式 用Python模拟一个客户端,结合打开一个HTML页面 创建客户端 #!/usr/bin/env python # -*- coding:utf8 -* ...
- 第四十节,requests模拟浏览器请求模块初识
requests模拟浏览器请求模块初识 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/latest/use ...
- HTTP协议简介详解 HTTP协议发展 原理 请求方法 响应状态码 请求头 请求首部 java模拟浏览器客户端服务端
协议简介 协议,自然语言里面就是契约,也是双方或者多方经过协商达成的一致意见; 契约也即类似于合同,自然有甲方123...,乙方123...,哪些能做,哪些不能做; 通信协议,也即是双方通过网络通信必 ...
随机推荐
- Mac OS X 10.9.3 UI 设置界面无法设置时区解决
10.9.3 在选项设置里无法设置时区,表现为选择时区的点的位置后无法保存,导致系统时间错乱,解决方法是用终端设置: sudo systemsetup -gettimezone sudo system ...
- Linux + Mono 目前已经支持Entity Framework 6.1
在上个随笔 CentOS上 Mono3.2.8运行ASP.NET MVC4经验中,步骤2中要求卸载EF 5.0,这样才能在Linux + Mono的环境中运行ASP.NET MVC4的Web应用.今天 ...
- hadoop 笔记(hbase)
hbase 基础: hbase是基于列的数据,其数据模式如下: 1.安装 1.1)hbase安装分为单机.伪分布式.分布式,单机下安装不依赖于hadoop:因为不需要分布式文件系统支持: 1.2)安装 ...
- java 多线程(ThreadPoolExecutor (补充))
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; impo ...
- Linux 网络编程(IO模型)
针对linux 操作系统的5类IO模型,阻塞式.非阻塞式.多路复用.信号驱动和异步IO进行整理,参考<linux网络编程>及相关网络资料. 阻塞模式 在socket编程(如下图)中调用如下 ...
- mysqlnd cannot connect to MySQL 4.1+
phpMyAdmin - error #2000 - mysqlnd cannot connect to MySQL 4.1+ using the old insecure authenticatio ...
- XSS危害——session劫持
在跨站脚本攻击XSS中简单介绍了XSS的原理及一个利用XSS盗取存在cookie中用户名和密码的小例子,有些同学看了后会说这有什么大不了的,哪里有人会明文往cookie里存用户名和密码.今天我们就介绍 ...
- 跟我一起云计算(4)——lucene
了解lucene的基本概念 这一部分可以参考我以前写的博客: http://www.cnblogs.com/skyme/tag/lucene/ lucene是什么 下图是一个很好的说明: 1.luce ...
- ASP.NET集成模式下的管道事件
- 自制操作系统(七) 加快中断处理,和加入FIFO缓冲区
参考书籍<30天自制操作系统>.<自己动手写操作系统> 2016-05-26.2016-07-09 主要是加快中断处理,和加入FIFO缓冲区. 因为之前是将打印字符的代码放在了 ...