1. private string HttpPost(string Url, string postDataStr)
  2. {
  3. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  4. request.Method = "POST";
  5. request.ContentType = "application/x-www-form-urlencoded";
  6. request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
  7. request.CookieContainer = cookie;
  8. Stream myRequestStream = request.GetRequestStream();
  9. StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
  10. myStreamWriter.Write(postDataStr);
  11. myStreamWriter.Close();
  12.  
  13. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  14.  
  15. response.Cookies = cookie.GetCookies(response.ResponseUri);
  16. Stream myResponseStream = response.GetResponseStream();
  17. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  18. string retString = myStreamReader.ReadToEnd();
  19. myStreamReader.Close();
  20. myResponseStream.Close();
  21.  
  22. return retString;
  23. }
  24.  
  25. public string HttpGet(string Url, string postDataStr)
  26. {
  27. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
  28. request.Method = "GET";
  29. request.ContentType = "text/html;charset=UTF-8";
  30.  
  31. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  32. Stream myResponseStream = response.GetResponseStream();
  33. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
  34. string retString = myStreamReader.ReadToEnd();
  35. myStreamReader.Close();
  36. myResponseStream.Close();
  37.  
  38. return retString;
  39. }

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. 使用sphinx索引mysql数据

    数据库表如下 mysql> select * from tb_account; +----+-------+------+ | id | name | age | +----+-------+- ...

  2. cmd命令 拷贝某文件夹及其子文件夹文件到其它文件夹

    @ECHO OFF cd/d %H:\FileLoc\CNET&cd.. ::echo 拷贝"%H:\FileLoc\CNET"中文件到"H:\FileLocTe ...

  3. IOS开发环境更换后重新制作Provisioning Profile证书详解

    新换了台Macbook,又折腾了一遍Provisioning Profile证书,苹果的证书繁锁复杂,每次制作都相当麻烦,而且Provisioning Profile证书是与设备绑定的,所以更换开发环 ...

  4. Android Json生成及解析实例

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  5. hdu-5082

    题意非常easy,就是给出父母的名字,然后依据父母的名字来给孩纸取名字! 能够将此题简化为: 孩纸的名字=父亲的frist name+字符串(_small_)+母亲额frist name; 然后将孩纸 ...

  6. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...

  7. DataTable 修改列名 删除列 调整列顺序

    DataTable myDt =dt;//删除列myDt.Columns.Remove("minArea");myDt.Columns.Remove("maxArea&q ...

  8. linux 分割文件

    import os import sysimport subprocess if len(sys.argv)<3 : print 'usage: filenum filename' file_n ...

  9. apktool 反翻译错误

    -出现 UndefinedResObject Exception : 这是因为被反编译的apk中有当前的framework不支持的属性:解决方式如下: 1.删除C:\Users\Administrat ...

  10. Android 汉字转拼音之工具篇

    /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Versi ...