C# 发起Get和Post请求
- public class ApiHelper
- {
//contentType application/json or application/xml- public string HttpGet(string Url, string contentType)
- {
- try
- {
- string retString = string.Empty;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- request.Method = "GET";
- request.ContentType = contentType;
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream myResponseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(myResponseStream);
- retString = streamReader.ReadToEnd();
- streamReader.Close();
- myResponseStream.Close();
- return retString;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static string HttpPost(string Url, string postDataStr, string contentType, out bool isOK)
- {
- string retString = string.Empty;
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
- request.Method = "POST";
- request.ContentType = contentType;
- request.Timeout = ;//设置超时时间
- request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
- Stream requestStream = request.GetRequestStream();
- StreamWriter streamWriter = new StreamWriter(requestStream);
- streamWriter.Write(postDataStr);
- streamWriter.Close();
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream);
- retString = streamReader.ReadToEnd();
- streamReader.Close();
- responseStream.Close();
- isOK = true;
- }
- catch (Exception ex)
- {
- if (ex.GetType() == typeof(WebException))//捕获400错误
- {
- var response = ((WebException)ex).Response;
- Stream responseStream = response.GetResponseStream();
- StreamReader streamReader = new StreamReader(responseStream);
- retString = streamReader.ReadToEnd();
- streamReader.Close();
- responseStream.Close();
- }
- else
- {
- retString = ex.ToString();
- }
- isOK = false;
- }
- return retString;
- }
- }
C# 发起Get和Post请求的更多相关文章
- 浏览器发起Get,Post请求时候传递的参数编码问题
浏览器发起Get,Post请求时候传递的参数编码问题 最近开发一个网站的时候,用了很多ajax方法,在页面发起Get,post请求,中间自然捎带有很多参数,有中文,有英文,英文一般是不存在编码问题的, ...
- Python向PHP发起GET与POST请求
CloudB项目中到PHP开发WEB管理端,用Python开发服务控制端,在项目中Python的服务控制端有时候须要主动连接PHP的WEB管理端下载或上传配置參数或数据信息,这里採用的原理是Pytho ...
- 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
本文由原作者松若章原创发布,作者主页:zhihu.com/people/hrsonion/posts,感谢原作者的无私分享. 1.引言 一道经典的面试题是:从 URL 在浏览器被被输入到页面展现的过程 ...
- python 爬虫 基于requests模块发起ajax的post请求
基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...
- python 爬虫 基于requests模块发起ajax的get请求
基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...
- Java基础/发起http和https请求
Java中发起http和https请求 一般调用外部接口会需要用到http和https请求. 本案例为:前后端完全分离,前端框架(React+Mobx+Nornj),后端(Go语言). 面临问题:跨域 ...
- 发起post、get请求
HttpURLConnection对象 /*** * 发起post请求,传输xml数据 * @param strUrl 请求地址 * @param xml 发送数据 * @return string ...
- golang使用http client发起get和post请求示例
[转自 http://www.01happy.com/golang-http-client-get-and-post/ ] get请求 get请求可以直接http.Get方法,非常简单. 1 2 3 ...
- async await 同时发起多个异步请求的方法
@action getBaseInfo = async() => { let baseInfo; try { baseInfo = await getBaseInfo(this.id); if ...
- 如何在java中发起http和https请求
一般调用外部接口会需要用到http和https请求. 一.发起http请求 1.写http请求方法 //处理http请求 requestUrl为请求地址 requestMethod请求方式,值为&qu ...
随机推荐
- 1393 0和1相等串 51nod
1393 0和1相等串 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给定一个0-1串,请找到一个尽可能长的子串,其中包含的0与1的个数相等. I ...
- Lifting the Stone 计算几何 多边形求重心
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- W5500中断寄存器的理解
W5500中断部分,W5500中文手冊V1.0 写的不够清楚,该文是本人结合中英文手冊及自己理解,整理出有关中断部分的理解,如有不对的请指正. 一:引脚 INTn 为中断输出(Interrupt ou ...
- 软件测试之怎么避免Bug漏测?
一.对需求评审阶段,对业务需求细节理解不明确,未深入挖掘隐含拓展需求 改进措施 需求评审前,我们应该先仔细阅读prd及交互文档,先形成自己对产品的思考,通过脑图的方式列出对产品设计的疑问点,从用户或者 ...
- lzugis——Arcgis Server for JavaScript API之自己定义InfoWindow
用过Arcgis Server for JavaScript API肯定知道InfoWIndow.你在用InfoWindow的时候会发现各种问题,比如不能全然显示的问题,遮盖对象的问题等等.所以呢我在 ...
- bzoj1898: [Zjoi2005]Swamp 沼泽鳄鱼
一眼矩乘 把图分成12个,然后直接搞. #include<cstdio> #include<iostream> #include<cstring> #include ...
- Android WiFi/WiFi热点开发总结
首先看一下WiFi的自我介绍: Wi-Fi是一种允许电子设备连接到一个无线局域网(WLAN)的技术,通常使用2.4G UHF或5G SHF ISM 射频频段.连接到无线局域网通常是有密码保护的:但也可 ...
- Java获取路径中的文件名(正则表达式)
Java获取路径中的文件名(正则表达式) 目标 在这个路径中我想得到model2 /E:/2017-02-21--SoftWare/github/test/Java/poiDemo_word2exce ...
- splunk的bucket组织目录——时间序列,按照时间来组织目录
splunk的bucket组织目录:db_1481515116_1480695302_0db_1481537316_1481532688_1db_1481547598_1481539988_2db_1 ...
- PCB Genesis增加点阵字 实现原理
我们采用Genesis增加点阵字时,用Genesis增加Canned Text即可,但奥宝中文不支持,且字符种类是有限的呀 不过没关系,没有自己造呀.在这里我分享一种增加点阵字的实现方法 一.通过代码 ...