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 ...
随机推荐
- 移动端 javascript 计算html font-size
直接上代码 (function(doc, win) { var docEl = doc.documentElement, resizeEvt ...
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...
- [CSS3] Use Sticky Positioning for Section Headers
We can take advantage of sticky positioning to keep a section header at the top of the page while th ...
- Android游戏框架Libgdx使用入门
转载自:http://blog.csdn.net/cping1982/article/details/6176191 Libgdx作者博客:http://www.badlogicgames.com/ ...
- HDOJ 5417 Victor and Machine 水
Victor and Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Othe ...
- android:怎样用一天时间,写出“飞机大战”这种游戏!(无框架-SurfaceView绘制)
序言作为一个android开发人员,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的. 体验地址:http://www.wandoujia. ...
- OC基础回想(十二)协议
在OC基础(十一)中我们讨论了类别和非正式协议的奇异之处.在使用非正式协议时.能够仅仅实现你想要获得响应的方法.也不必在对象中声明不论什么内容来表示该对象可用作托付对象. 全部这些任务能够用最少的代码 ...
- python 005 正则表达式
. 任意字符 ^ 匹配字符串开始 $ 匹配字符串结尾 * 匹配前面出现了零次或多次 + 匹配前面出现了一次或多次 ? 匹配前面出现零次或一次 {N} 匹配前面出现了N次 {M,N} 匹配重复出现M-N ...
- RabbitMQ基本管理(下)
为了可以登陆RabbitMQ,必须创建RabbitMQ用户账号. # rabbitmqctl add_user elite elite123 Creating user "elite&quo ...
- 使用Carthage安装及使用第三方库
CocoaPods 安装不了,只好使用Carthage 参考:http://www.jianshu.com/p/52dff4cef8a2 http://www.jianshu.com/p/bf263c ...