POST请求

/// <summary>
/// POST请求获取信息
/// </summary>
/// <param name="url"></param>
/// <param name="paramData"></param>
/// <returns></returns>
public string POST(string url, string paramData, int timeout = , List<System.Net.Cookie> cookies = null)
{
string ret = string.Empty;
try
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc;
webReq.ProtocolVersion = HttpVersion.Version10;
}
SetProxy(ref webReq);
webReq.Method = "POST";
webReq.ContentType = "application/json; charset=utf-8";
webReq.ServicePoint.Expect100Continue = false;
//webReq.ContentType = "text/html;charset=utf-8";
webReq.Timeout = timeout;
webReq.ContentLength = byteArray.Length; if (!string.IsNullOrEmpty(UserAgent))
webReq.UserAgent = UserAgent;
if (cookies != null && cookies.Count > )
{
webReq.CookieContainer = new CookieContainer(); string host = new Uri(url).Host;
foreach (System.Net.Cookie c in cookies)
{
c.Domain = host;
webReq.CookieContainer.Add(c);
}
}
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, , byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch (Exception ex)
{
ClassLoger.Error(ex, "HttpUtils/POST", url);
throw ex;
}
return ret;
}

GET请求

        /// <summary>
/// GET请求获取信息
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string GET(string url, int timeout = , List<System.Net.Cookie> cookies = null)
{
string ret = string.Empty;
try
{
HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url);
if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc;
web.ProtocolVersion = HttpVersion.Version10;
}
SetProxy(ref web);
web.Method = "GET";
web.Timeout = timeout;
if (!string.IsNullOrEmpty(UserAgent))
web.UserAgent = UserAgent;
if (cookies != null && cookies.Count > )
{
web.CookieContainer = new CookieContainer(); string host = new Uri(url).Host;
foreach (System.Net.Cookie c in cookies)
{
c.Domain = host;
web.CookieContainer.Add(c);
}
}
HttpWebResponse res = (HttpWebResponse)web.GetResponse();
Stream s = res.GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.UTF8);
ret = sr.ReadToEnd();
sr.Close();
res.Close();
s.Close();
//ClassLoger.Error("HttpUtils/GET",url+" "+ ret);
}
catch (Exception ex)
{
ClassLoger.Error(ex, "HttpUtils/GET", url);
throw ex;
}
return ret;
}

C#HTTP请求之POST请求和GET请求的更多相关文章

  1. axios中出现两次请求,OPTIONS请求和GET请求

    在项目中发现ajax中出现两次请求,OPTIONS请求和GET请求 查看到浏览器NetWork有两次请求,请求url一样: 查找原因是浏览器对简单跨域请求和复杂跨域请求的处理区别. XMLHttpRe ...

  2. 转:PHP中的使用curl发送请求(GET请求和POST请求)

    原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...

  3. 简单介绍HTTP的请求(get请求和post请求)以及对应的响应的内容

    链接解析: https://oa.hbgf.net.cn/login.jsp;jsessionid=47084322738F8DB18D60752944DFD1AA http或者https表示使用的是 ...

  4. 浅说Get请求和Post请求

    Web 上最常用的两种 Http 请求就是 Get 请求和 Post 请求了.我们在做 java web 开发时,也总会在 servlet 中通过 doGet 和 doPost 方法来处理请求:更经常 ...

  5. JSP、Servlet中get请求和post请求的区别总结

    在学习JavaWeb最初的开始阶段,大家都会遇到HttpServlet中的doGet和doPost方法.前两天看<Head First Servlets & JSP>看到其中讲关于 ...

  6. Ajax中get请求和post请求

    我们在使用Ajax向服务器发送数据时,可以采用Get方式请求服务器,也可以使用Post方式请求服务器,那么什么时候该采用Get方式,什么时候该采用Post方式呢? Get请求和Post请求的区别: 1 ...

  7. slave IO流程之二:注册slave请求和dump请求

    slave IO流程已经在http://www.cnblogs.com/onlyac/p/5815566.html中有介绍 这次我们要探索注册slave请求和dump请求的报文格式和主要流程. 一.注 ...

  8. loadrunner录制脚本如何选择使用get请求和post请求的方式

    在loadrunner工具里录制脚本时常常会用到get请求和post请求,有关loadrunner常用的这两类的请求主要有: get请求: web_url 和 web_link post请求: web ...

  9. iOS开发网络篇—GET请求和POST请求

    iOS开发网络篇—GET请求和POST请求 一.GET请求和POST请求简单说明 创建GET请求 // 1.设置请求路径 NSString *urlStr=[NSString stringWithFo ...

随机推荐

  1. Python中函数和方法的区别

    方法是一种特殊的函数属于某个类的的函数叫方法不属于某个类的函数叫函数 转自csdn https://blog.csdn.net/weixin_40380298/article/details/7825 ...

  2. phxpaxos遇到反复拉取checkpoint但是反复失败的问题,给其它节点造成压力

    原因: 接收checkpoint时与接收普通message共用IOLoop中的队列,当遇到队列满或者超内存时,会造成checkpoint的包随机丢失的问题 解决办法: 遇到checkpoint时不丢弃 ...

  3. Java8自定义函数式编程接口和便捷的引用类的构造器及方法

    什么是函数编程接口? 约束:抽象方法有且只有一个,即不能有多个抽象方法,在接口中覆写Object类中的public方法(如equal),不算是函数式接口的方法. 被@FunctionalInterfa ...

  4. 7、...arg ...[1,2,3] 数组扩展

    1.将离散的参数转成数组 2.将数组拆成单个离散的值 https://blog.csdn.net/qq_30100043/article/details/53391308 箭头函数写法 函数名 -&g ...

  5. IDEA查看项目对应的git地址

    参考 https://blog.csdn.net/yyyadan/article/details/85091972 项目文件夹/.git/config

  6. hbase基础建表语句

    在Hadoop目录下的HBASE下执行命令 ./hbase shell 进入hbase环境 创建hbase 数据库表 create "表名", "字段A",&q ...

  7. 使用FFmpeg解码并用swscale将YUV转为RGB

    #include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h&g ...

  8. TZOJ 2289 Help Bob(状压DP)

    描述 Bob loves Pizza but is always out of money. One day he reads in the newspapers that his favorite ...

  9. 摘选改善Python程序的91个建议2

       62.metaclass stackflow          中文翻译    63.Python对象协议   https://zhuanlan.zhihu.com/p/26760180     ...

  10. webapp优化

    1. 优化前提: 业务架构与数据库设计 2.  单页web应用  : ExtJs  backbone  ng  avalon 框架: React Native    , ionic  , Mui, m ...