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 ...
随机推荐
- MySQL终端(Terminal)命令基本操作(转)
注意:MySQL数据库命令不区分大小写.但在MAC的终端,如果你想使用tab自动补全命令,那么你就必须使用大写,这样MAC的终端才会帮你补全命令,否则你按N遍tab都不会有响应. 1.数据库(data ...
- hibernate使用c3p0数据源
在配置好hibernate连接数据库环境的前提下,我们进行例如以下操作就能够搭建好hibernate中使用c3p0数据源的环境了. 1). 导入 jar 包: hibernate-release-4. ...
- 查看编译器的默认include 路径
echo | gcc -v -x c++ -E - echo | g++ -v -x c++ -E - `gcc -print-prog-name=cc1plus` -v `g++ -print-pr ...
- 把握linux内核设计思想系列
[版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途] 本专栏分析linux内核的设计实现,包含系统调用.中断.下半部机制.时间管理. ...
- 最全Linux 与 Linux Windows 文件共享
前提说明: windows主机信息:192.168.1.100 帐号:abc password:123 共享目录:share linux主机信息:192.168.1.200 帐号:def passwo ...
- SGU - 403 - Scientific Problem (水)
403. Scientific Problem Time limit per test: 0.25 second(s) Memory limit: 65536 kilobytes input: sta ...
- Java 二进制和十进制互转,二进制和BitSet互转
/** * 二进制转十进制 * * @param binaryNumber * @return */ public static int binaryToDecimal(int binaryNumbe ...
- Codeforces--598A--Tricky Sum(数学)
Tricky Sum Tricky SumCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- RPC通信框架——RCF介绍
现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...
- 85.Ext.Window
转自:https://chenjumin.iteye.com/blog/668421 1.主要配置项: closable:是否允许关闭窗口,默认为true. closeActi ...