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请求的更多相关文章

  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. MySQL的limit用法及优化(转)

    常规用法: 用法一: OFFSET ; 比如这个SQL ,limit后面跟的是2条数据,offset后面是从第1条开始读取. 用法二: ,; 而这个SQL,limit后面是从第2条开始读,读取1条信息 ...

  2. 让我们加密吧Let's encrypt

    前言 如今的互联网越来越不安全,我们每个人的信息以及隐私不断被暴露,地下黑产盛行,经常收到垃圾短信,经常被陌生人的电话骚扰.我们的个人信息是怎么泄漏的呢?各种爬虫无时无刻不在互联网爬取着信息,各种嗅探 ...

  3. java package 命名空间

    原文: http://www.studytonight.com/java/package-in-java.php 创建一个简单的maven 项目的命令是: mvn  archetype:generat ...

  4. Spring MVC新手教程(一)

    直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...

  5. 【SSH之旅】一步步学习Hibernate框架(一):关于持久化

    在不引用不论什么框架下,我们会通过平庸的代码不停的对数据库进行操作,产生了非常多冗余的可是又有规律的底层代码,这样频繁的操作数据库和大量的底层代码的反复书写极大的浪费了程序人员的书写.就在这样一种情况 ...

  6. Android之自己定义(上方标题随ViewPager手势慢慢滑动)

    近期非常蛋疼,项目要模仿网易新闻的样式去做.上次把仿网易新闻client的下拉刷新写出来了.这次是ViewPager的滑动,同一时候ViewPager的上面标题下划线尾随者移动.本来通过ViewPag ...

  7. openStack enscaption

  8. Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements

    Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements 开始想写一个 ...

  9. Dirichlet's Theorem on Arithmetic Progressions

    http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...

  10. [Apple开发者帐户帮助]二、管理你的团队(3)删除团队成员

    如果您已加入Apple开发者计划,您将在App Store Connect中管理团队成员.有关详细信息,请转到App Store Connect帮助中的添加和编辑用户. 如果您已加入Apple Dev ...