private string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close(); return retString;
} 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;
}

C#代码模拟http发送get和post请求的更多相关文章

  1. 使用java程序模拟页面发送http的post请求

    在web应用程序中,一般都是通过页面发送http的post请求,但也可以使用java程序来模拟页面发送请求,代码如下: import java.io.BufferedReader; import ja ...

  2. C#模拟http 发送post或get请求

    /// <summary> /// 模拟HTTP提交表单并获取返回数据 /// POST /// </summary> /// <param name="Url ...

  3. 【转】C#模拟http 发送post或get请求

    原文地址:http://www.cnblogs.com/xssxss/archive/2012/07/03/2574554.html 模拟POST Json public static string ...

  4. 使用appium模拟用户发送短信

    一段简单粗糙的代码.主要是实现的功能是模拟用户发送短信的功能. python版本3.5.2 appium版本1.4.16.1 from appium import webdriver desired_ ...

  5. C#模拟httpwebrequest请求_向服务器模拟cookie发送

    使用C#代码模拟web请求,是一种常用的方法,以前没专门整理过,这里暂时贴上自己整理的完整代码,以后再做梳理: public class MyRequest { #region 辅助方法 public ...

  6. php使用curl模拟多线程发送请求

    每个PHP文件的执行是单线程的,但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程,这里用curl模拟多线程发送请求.php的curl多线程是通过不断调用curl_multi_e ...

  7. 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试

    在调用公司的某个接口时,直接通过浏览器配置代理服务器可以请求到如下数据: 请求url地址:http://wwwnei.xuebusi.com/rd-interface/getsales.jsp?cid ...

  8. php模拟发送GET和POST请求

    php分别模拟发送GET和POST请求,非常实用的额,也可作PHP CURL入门级的理解教材的,示例代码如下: <?php /* ** php分别模拟发送GET与POST请求 ** */ fun ...

  9. 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 ...

随机推荐

  1. nodejs上传图片模块做法;

    服务端代码: var express = require('express'); var swig = require('swig'); //1.引入multer模块 var multer = req ...

  2. 小话python 中的编码转换

    1.前言: 一直认为自己会了,也明白了其中的知识,但是过几天不用就马上忘记了,总不能天天复习吧!还是来个好记性不如烂笔头吧! 2.编码: python解释器在加载 .py 文件中的代码时,会对内容进行 ...

  3. Saiku国际化总结

    国际化步骤: 1.在mondrian.properties同路径下加上locale_zh_CN.properties资源文件,内容例如:schema.name.K12UserAnalysis=K12用 ...

  4. Android自定义Activity酷炫的动画跳转效果

    两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class);   sta ...

  5. oracle添加用户及权限

    CREATE USER qdcenter 用户名 IDENTIFIED BY qdcenter   密码 DEFAULT TABLESPACE data1  默认表空间 TEMPORARY TABLE ...

  6. BFC / hasLayout

    BFC - block formatting context 1.float的值不能为none 2.overflow的值不能为visible 3.display的值为table-cell,table- ...

  7. Javascript/Jquery 中each() 和forEach()的区别

    从名字看上去这两个方法好像有点关系,但在javascript中它们区别还是挺大的. forEach() 用于数组的操作,对数组中的每个元素执行制定的函数(不是数组不能使用forEach()方法). 而 ...

  8. .aspx.cs传值与取值

    1:.aspx中post传值 $.post("ABP_ExchangeRatelz.aspx", { option: "isdelete", Ori_Curre ...

  9. WCF Service Configuration Editor的使用

    原文:http://www.cnblogs.com/Ming8006/p/3772221.html 通过WCF Service Configuration Editor的配置修改Client端 参考 ...

  10. Scala io操作

    1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...