1. 需要的库类

\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll
System.Net.Http.HttpClient

2. 代码

public class HttpClientHelper
{
private static readonly object LockObj = new object();
private static HttpClient client = null;
public HttpClientHelper()
{
GetInstance();
} public static HttpClient GetInstance()
{
if (client == null)
{
lock (LockObj)
{
if (client == null)
{
client = new HttpClient();
}
}
}
return client;
} public async Task<string> PostAsync(string url, string strJson)//post异步请求方法
{
try
{
HttpContent content = new StringContent(strJson);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
//由HttpClient发出异步Post请求
HttpResponseMessage res = await client.PostAsync(url, content);
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
string str = res.Content.ReadAsStringAsync().Result;
return str;
}
else
return null;
}
catch (Exception ex)
{
return null;
}
} public string Post(string url, string strJson)//post同步请求方法
{
try
{
HttpContent content = new StringContent(strJson);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
//client.DefaultRequestHeaders.Connection.Add("keep-alive");
//由HttpClient发出Post请求
Task<HttpResponseMessage> res = client.PostAsync(url, content);
if (res.Result.StatusCode == System.Net.HttpStatusCode.OK)
{
string str = res.Result.Content.ReadAsStringAsync().Result;
return str;
}
else
return null;
}
catch (Exception ex)
{
return null;
}
} public string Get(string url)
{
try
{
var responseString = client.GetStringAsync(url);
return responseString.Result;
}
catch (Exception ex)
{
return null;
}
}
}

3. Http 系列

3.1 发起请求

使用 HttpWebRequest 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501036.html

使用 WebClient 发起 Http 请求 :https://www.cnblogs.com/MichaelLoveSna/p/14501582.html

使用 HttpClient 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501592.html

使用 HttpClient 发起上传文件、下载文件请求:https://www.cnblogs.com/MichaelLoveSna/p/14501603.html

3.2 接受请求

使用 HttpListener 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501628.html

使用 WepApp 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501612.html

使用 WepApp 处理文件上传、下载请求:https://www.cnblogs.com/MichaelLoveSna/p/14501616.html

C# 应用 - 使用 HttpClient 发起 Http 请求的更多相关文章

  1. httpclient发起https请求以及获取https返回内容

    工作中的需要,使用Apache httpclient发起获取通过https返回的内容,试了很多网上的解决办法都不行,查阅了Apache httpclient的官方文档,最后终于找出解决方法,分享给需要 ...

  2. 通过HttpClient发起Get请求,获取Json数据,然后转为java数据,然后批量保存数据库;

    Json转java所需Jar包: commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,comm ...

  3. 利用httpClient发起https请求

    HttpClientBuilder b = HttpClientBuilder.create();// setup a Trust Strategy that allows all certifica ...

  4. C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求

    1. 示例代码 using System; using System.IO; using System.Net.Http; /// <summary> /// 下载文件 /// </ ...

  5. [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式

    1.利用apache提供的commons-httpclient-3.0.jar包 代码如下: /** * 利用HttpClient发起POST请求,并接收返回的响应内容 * * @param url ...

  6. C# 应用 - 使用 HttpWebRequest 发起 Http 请求

    helper 类封装 调用 1. 引用的库类 \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll Syste ...

  7. C# 应用 - 使用 WebClient 发起 Http 请求

    1. 需要的库类 \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.dll System.Net.WebCli ...

  8. android4.0 HttpClient 以后不能在主线程发起网络请求

    android4.0以后不能在主线程发起网络请求,该异步网络请求. new Thread(new Runnable() { @Override public void run() { // TODO ...

  9. HttpClient发起Http/Https请求工具类

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

随机推荐

  1. C#枚举(一)使用总结以及扩展类分享

    0.介绍 枚举是一组命名常量,其基础类型为任意整型. 如果没有显式声明基础类型, 则为Int32 在实际开发过程中,枚举的使用可以让代码更加清晰且优雅. 最近在对枚举的使用进行了一些总结与整理,也发现 ...

  2. docker的底层-隔离的核心

    在了解底层原理之前: 说几个名词: 解耦状态: 所有东西都没有重复,任何东西都没有公用的地方. 半解耦状态:有部分共同的一起用,其他的独立 完全解耦状态: 就是各自都是独立没有重复. kvm:完全解耦 ...

  3. leetcode 22. 括号生成 dfs

    先思考符合要求的串是什么样子的 任意时刻,(数量大于),且最后(==)==n即可 考虑下一个加入string的字符时(或者)即可 dfs class Solution { public: vector ...

  4. Leetcode(53)-最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  5. mybatis(十一)mybatis常见问题

    用注解还是用 xml 配置? 常用注解:@Insert.@Select.@Update.@Delete.@Param.@Results. @Result 在 MyBatis 的工程中,我们有两种配置 ...

  6. 无需扫描即可查找和攻击域SQL Server (SPN)

    无扫描SQL Server发现简介 当您没有凭据或正在寻找不在域中的SQL Server时,使用各种扫描技术来查找SQL Server可能非常有用.但是,此过程可能很嘈杂,耗时,并且可能由于子网未知, ...

  7. React Hooks: useDebugValue All In One

    React Hooks: useDebugValue All In One useDebugValue https://reactjs.org/docs/hooks-reference.html#us ...

  8. how to config custom process.env in node.js

    how to config custom process.env in node.js process.env APP_ENV NODE_ENV https://nodejs.org/api/proc ...

  9. empty Checker

    empty Checker "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * ...

  10. EventEmitter & custom events & sub/pub

    EventEmitter & custom events & sub/pub https://repl.it/@xgqfrms/PPLabs-frontend-answers // 5 ...