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 ...
随机推荐
- OpenCV使用说明
我在这边大概说一下OpenCV的使用,具体环境配置参考下面我给出的两个链接. 1. 对于目前OpenCV来说,安装变的简单了很多,现在官方已经给出了预编译文件,不要重新编译.具体使用可以参考http: ...
- js 推断字符串是否包括某字符串
var Cts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包括Text字符串'); } ...
- POJ 3748:位操作
位操作 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8964 Accepted: 3581 Description 如 ...
- scikit-learn:通过Non-negative matrix factorization (NMF or NNMF)实现LSA(隐含语义分析)
之前写过两篇文章.各自是 1)矩阵分解的综述:scikit-learn:2.5.矩阵因子分解问题 2)关于TruncatedSVD的简介:scikit-learn:通过TruncatedSVD实现LS ...
- Jemeter命令执行
http://mp.weixin.qq.com/s?__biz=MzAxOTg2NDUyOA==&mid=2657555034&idx=1&sn=9e6a3fbd5eed859 ...
- 2014年百度之星程序设计大赛 - 资格赛 第一题 Energy Conversion
小记:long long %I64d 代码: #include <iostream> #include <stdio.h> #include <string.h> ...
- Google Code Jam在线測试题目--Alien Language
Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...
- git fetch批处理,遍历一个文件夹下的所有子目录,执行git fetch --all
echo off for /d %%i in (*) do ( echo %%i cd %%i git fetch --all cd .. ) 判断子目录是否有.git文件夹 echo off for ...
- C# Path 有关于文件路径等问题类(转)
C# Path 标签:C#, Path C-Sharp 0 Path handles file path processing. The .NET Framework provides effect ...
- jdbc 接口学习笔记
一.JDBC概念 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系型数据库提供统一访问,它由一组用Jav ...