1. public class ApiHelper
  2. {
         //contentType application/json or application/xml
  3. public string HttpGet(string Url, string contentType)
  4. {
  5. try
  6. {
  7. string retString = string.Empty;
  8.  
  9. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  10. request.Method = "GET";
  11. request.ContentType = contentType;
  12.  
  13. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  14. Stream myResponseStream = response.GetResponseStream();
  15. StreamReader streamReader = new StreamReader(myResponseStream);
  16. retString = streamReader.ReadToEnd();
  17. streamReader.Close();
  18. myResponseStream.Close();
  19. return retString;
  20. }
  21. catch (Exception ex)
  22. {
  23. throw ex;
  24. }
  25. }
  26.  
  27. public static string HttpPost(string Url, string postDataStr, string contentType, out bool isOK)
  28. {
  29. string retString = string.Empty;
  30.  
  31. try
  32. {
  33. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
  34. request.Method = "POST";
  35. request.ContentType = contentType;
  36. request.Timeout = ;//设置超时时间
  37. request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
  38. Stream requestStream = request.GetRequestStream();
  39. StreamWriter streamWriter = new StreamWriter(requestStream);
  40. streamWriter.Write(postDataStr);
  41. streamWriter.Close();
  42.  
  43. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  44.  
  45. Stream responseStream = response.GetResponseStream();
  46. StreamReader streamReader = new StreamReader(responseStream);
  47. retString = streamReader.ReadToEnd();
  48. streamReader.Close();
  49. responseStream.Close();
  50.  
  51. isOK = true;
  52. }
  53. catch (Exception ex)
  54. {
  55. if (ex.GetType() == typeof(WebException))//捕获400错误
  56. {
  57. var response = ((WebException)ex).Response;
  58. Stream responseStream = response.GetResponseStream();
  59. StreamReader streamReader = new StreamReader(responseStream);
  60. retString = streamReader.ReadToEnd();
  61. streamReader.Close();
  62. responseStream.Close();
  63. }
  64. else
  65. {
  66. retString = ex.ToString();
  67. }
  68. isOK = false;
  69. }
  70.  
  71. return retString;
  72. }
  73. }

C# 发起Get和Post请求的更多相关文章

  1. 浏览器发起Get,Post请求时候传递的参数编码问题

    浏览器发起Get,Post请求时候传递的参数编码问题 最近开发一个网站的时候,用了很多ajax方法,在页面发起Get,post请求,中间自然捎带有很多参数,有中文,有英文,英文一般是不存在编码问题的, ...

  2. Python向PHP发起GET与POST请求

    CloudB项目中到PHP开发WEB管理端,用Python开发服务控制端,在项目中Python的服务控制端有时候须要主动连接PHP的WEB管理端下载或上传配置參数或数据信息,这里採用的原理是Pytho ...

  3. 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?

    本文由原作者松若章原创发布,作者主页:zhihu.com/people/hrsonion/posts,感谢原作者的无私分享. 1.引言 一道经典的面试题是:从 URL 在浏览器被被输入到页面展现的过程 ...

  4. python 爬虫 基于requests模块发起ajax的post请求

    基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...

  5. python 爬虫 基于requests模块发起ajax的get请求

    基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...

  6. Java基础/发起http和https请求

    Java中发起http和https请求 一般调用外部接口会需要用到http和https请求. 本案例为:前后端完全分离,前端框架(React+Mobx+Nornj),后端(Go语言). 面临问题:跨域 ...

  7. 发起post、get请求

    HttpURLConnection对象 /*** * 发起post请求,传输xml数据 * @param strUrl 请求地址 * @param xml 发送数据 * @return string ...

  8. golang使用http client发起get和post请求示例

    [转自 http://www.01happy.com/golang-http-client-get-and-post/ ] get请求 get请求可以直接http.Get方法,非常简单. 1 2 3 ...

  9. async await 同时发起多个异步请求的方法

    @action getBaseInfo = async() => { let baseInfo; try { baseInfo = await getBaseInfo(this.id); if ...

  10. 如何在java中发起http和https请求

    一般调用外部接口会需要用到http和https请求. 一.发起http请求 1.写http请求方法 //处理http请求 requestUrl为请求地址 requestMethod请求方式,值为&qu ...

随机推荐

  1. 1393 0和1相等串 51nod

    1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...

  2. Lifting the Stone 计算几何 多边形求重心

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  3. W5500中断寄存器的理解

    W5500中断部分,W5500中文手冊V1.0 写的不够清楚,该文是本人结合中英文手冊及自己理解,整理出有关中断部分的理解,如有不对的请指正. 一:引脚 INTn 为中断输出(Interrupt ou ...

  4. 软件测试之怎么避免Bug漏测?

    一.对需求评审阶段,对业务需求细节理解不明确,未深入挖掘隐含拓展需求 改进措施 需求评审前,我们应该先仔细阅读prd及交互文档,先形成自己对产品的思考,通过脑图的方式列出对产品设计的疑问点,从用户或者 ...

  5. lzugis——Arcgis Server for JavaScript API之自己定义InfoWindow

    用过Arcgis Server for JavaScript API肯定知道InfoWIndow.你在用InfoWindow的时候会发现各种问题,比如不能全然显示的问题,遮盖对象的问题等等.所以呢我在 ...

  6. bzoj1898: [Zjoi2005]Swamp 沼泽鳄鱼

    一眼矩乘 把图分成12个,然后直接搞. #include<cstdio> #include<iostream> #include<cstring> #include ...

  7. Android WiFi/WiFi热点开发总结

    首先看一下WiFi的自我介绍: Wi-Fi是一种允许电子设备连接到一个无线局域网(WLAN)的技术,通常使用2.4G UHF或5G SHF ISM 射频频段.连接到无线局域网通常是有密码保护的:但也可 ...

  8. Java获取路径中的文件名(正则表达式)

    Java获取路径中的文件名(正则表达式) 目标 在这个路径中我想得到model2 /E:/2017-02-21--SoftWare/github/test/Java/poiDemo_word2exce ...

  9. splunk的bucket组织目录——时间序列,按照时间来组织目录

    splunk的bucket组织目录:db_1481515116_1480695302_0db_1481537316_1481532688_1db_1481547598_1481539988_2db_1 ...

  10. PCB Genesis增加点阵字 实现原理

    我们采用Genesis增加点阵字时,用Genesis增加Canned Text即可,但奥宝中文不支持,且字符种类是有限的呀 不过没关系,没有自己造呀.在这里我分享一种增加点阵字的实现方法 一.通过代码 ...