C#模拟http 发送post或get请求
/// <summary>
/// 模拟HTTP提交表单并获取返回数据
/// POST
/// </summary>
/// <param name="Url">提交地址</param>
/// <param name="postDataStr">参数</param>
/// <param name="cookies">cookies</param>
/// <returns></returns>
public string HttpPost(string Url, string postDataStr, CookieCollection cookies)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36";
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
using (StreamWriter myStreamWriter = new StreamWriter(request.GetRequestStream(), Encoding.GetEncoding("utf-8")))
{
myStreamWriter.Write(postDataStr);
myStreamWriter.Flush();
} HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
response.Close(); return retString;
} /// <summary>
/// 模拟HTTP提交表单并获取返回数据
/// GET
/// </summary>
/// <param name="Url">提交地址</param>
/// <param name="postDataStr">参数</param>
/// <returns></returns>
public string HttpGet(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
}
在post的时候有时也用的到cookie,像登录163发邮件时候就需要发送cookie,所以在外部一个cookie属性随时保存 CookieContainer cookie = new CookieContainer();
*注意:有时候请求会重定向,但我们就需要从重定向url获取东西,像QQ登录成功后获取sid,但上面的会自动根据重定向地址跳转。我们可以用:
request.AllowAutoRedirect = false;设置重定向禁用,你就可以从headers的Location属性中获取重定向地址。
C#模拟http 发送post或get请求的更多相关文章
- 使用java程序模拟页面发送http的post请求
在web应用程序中,一般都是通过页面发送http的post请求,但也可以使用java程序来模拟页面发送请求,代码如下: import java.io.BufferedReader; import ja ...
- 【转】C#模拟http 发送post或get请求
原文地址:http://www.cnblogs.com/xssxss/archive/2012/07/03/2574554.html 模拟POST Json public static string ...
- C#代码模拟http发送get和post请求
private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)W ...
- php使用curl模拟多线程发送请求
每个PHP文件的执行是单线程的,但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程,这里用curl模拟多线程发送请求.php的curl多线程是通过不断调用curl_multi_e ...
- 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
在调用公司的某个接口时,直接通过浏览器配置代理服务器可以请求到如下数据: 请求url地址:http://wwwnei.xuebusi.com/rd-interface/getsales.jsp?cid ...
- php模拟发送GET和POST请求
php分别模拟发送GET和POST请求,非常实用的额,也可作PHP CURL入门级的理解教材的,示例代码如下: <?php /* ** php分别模拟发送GET与POST请求 ** */ fun ...
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- Ajax详解及其案例分析------如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表
本节主要内容预览: 1 获得Ajax对象 2 使用Ajax对象发送GET请求 3 使用Ajax对象发送POST请求 4 使用Ajax校验用户名 5 POST请求时的乱码处理 6 GET请求时的乱码处理 ...
随机推荐
- css3 自定义字体 @font-face
CSS3 @font-face 规则 在 CSS3 之前,web 设计师必须使用已在用户计算机上安装好的字体. 通过 CSS3,web 设计师可以使用他们喜欢的任意字体. 当您您找到或购买到希望使用的 ...
- 多个html怎么导入相同的头部导航
1. iframe 包含法.页头和页尾分别做成一个页面,然后通过iframe嵌入到调用的页面.这种方法在页头页尾高度固定的时候比较适用,因为当页头页尾高度不固定时,需要iframe根据页面内容自适应高 ...
- MVVM页面跳转 技巧性标记
刚学MVVM 百度了很多概念性的东西 也参考了网上的例子 基本有了了解 但是我发现 我做了一个登录页面以后 我跳转咋办呢? VM里面咋做跳转? 问了一下其他的群友得到了一些启发.感谢“上海*松” 我仅 ...
- C# 取整函数
向上取整math.ceiling(1) = 1math.ceiling(1.1) = 2math.ceiling(1.5) = 2向下取整math.float(1) = 1math.float(1.1 ...
- Python-类的属性
类的属性,可以称为成员变量 类的方法,可以称为成员函数 对象的创建 - 创建对象的过程称之为实例化:当一个对象被创建后,包含三个方面的特性:对象句柄.属性和方法. - 句柄用于区分不同的对象(实例 ...
- 使用git grep进行git搜索
1.git grep foo 会自动map所有包含foo的文件 2.git grep -n foo 显示行号 3.git grep --name-only foo 只显示文件名 4.git grep ...
- Hibernate注解方式配置-继承关系
在JPA中,实体继承关系的映射策略共有三种:单表继承策略(table per class).Joined策略(table per subclass)和Table_PER_Class策略. 1.单表继承 ...
- 【MongoDB初识】-条件操作符
1.条件>,<,>=,<=在MongoDB中的写法 >:$gt,<:$lt,>=:$gte,<=:$lte,<>:$ne 具体使用方法: d ...
- 通用js地址选择器
用js实现通用的地址选择器,省份,城市,地区自动关联更新 点击下面查看详细代码: http://runjs.cn/code/s8sqkhcv 关键地址库代码: var addr_arr = new A ...
- [转]oracle 分析函数over
oracle 分析函数over 分析函数(OVER) 目录: =============================================== 1.Oracle分析函数简介 2. O ...