/// <summary>
  /// HttpClient实现Post请求
/// </summary>
static async void dooPost()
{
string url = "http://localhost:52824/api/register";
//设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{ {"Id",""},
{"Name","添加zzl"},
{"Info", "添加动作"}//键名必须为空
}); //await异步等待回应 var response = await http.PostAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
} }
/// <summary>
/// HttpClient实现Get请求
/// </summary>
static async void dooGet()
{
string url = "http://localhost:52824/api/register?id=1";
//创建HttpClient(注意传入HttpClientHandler)
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler))
{
//await异步等待回应
var response = await http.GetAsync(url);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
/// <summary>
/// HttpClient实现Put请求
/// </summary>
static async void dooPut()
{
var userId = ;
string url = "http://localhost:52824/api/register?userid=" + userId; //设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"Name","修改zzl"},
{"Info", "Put修改动作"}//键名必须为空
}); //await异步等待回应 var response = await http.PutAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Console.WriteLine(await response.Content.ReadAsStringAsync());
} }
 
   /// <summary>
/// HttpClient异步请求
/// </summary>
/// <param name="uri"></param>
/// <param name="content"></param>
/// <returns></returns>
private static async Task<HttpResponseMessage> SendAsync(string uri, string content)
{
HttpContent strContent = new StringContent(content);
strContent.Headers.Clear(); strContent.Headers.Add("Content-Type", "application/soap+xml;charset=utf-8");//"application/soap+xml; charset=utf-8"
//strContent.Headers.Add("Content-Length", content.Length.ToString());
HttpClient client = new HttpClient();
HttpResponseMessage task=await client.PostAsync(uri, strContent); return task;
}
//调用如下:
   static void Main(string[] args)
        {
            var soap = SOAPMessage("testcase009", "123456", "hello,【华信】", "", DateTime.Now.AddYears(-1));
            var rlt = SendAsync("http://192.168.1.22/webservice.asmx", soap);
            Console.WriteLine(rlt.Result.Content.ReadAsStringAsync().Result);
            Console.ReadKey();
        }

HttpClient请求发送的几种用法:的更多相关文章

  1. HttpClient请求发送的几种用法二:

    public class HttpClientHelper    {        private static readonly HttpClientHelper _instance = new H ...

  2. JAVA发送HttpClient请求及接收请求结果

    1.写一个HttpRequestUtils工具类,包括post请求和get请求 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...

  3. JAVA发送HttpClient请求及接收请求结果过程

    1.写一个HttpRequestUtils工具类,包括post请求和get请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  4. 关于HttpClient,HttpURLConnection,OkHttp的用法

    1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...

  5. 转 关于HttpClient,HttpURLConnection,OkHttp的用法

    转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...

  6. android httpClient 支持HTTPS的2种处理方式

    摘自: http://www.kankanews.com/ICkengine/archives/9634.shtml 项目中Android https或http请求地址重定向为HTTPS的地址,相信很 ...

  7. JAVA EE 项目常用知识 之AJAX技术实现select下拉列表联动的两种用法(让你真正理解ajax)

    ajax 下拉列表联动的用法. ajax的定义: AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的 ...

  8. C# HttpClient 请求认证、数据传输笔记

    目录 一,授权认证 二,请求类型 三,数据传输 C# HttpClient 请求认证.数据传输笔记 一,授权认证 客户端请求服务器时,需要通过授权认证许可,方能获取服务器资源,目前比较常见的认证方式有 ...

  9. .NET Core HttpClient请求异常详细情况分析

    前言 最近项目上每天间断性捕获到HttpClient请求异常,感觉有点奇怪,于是乎观察了两三天,通过日志以及对接方沟通确认等等,查看对应版本源码,尝试添加部分配置发布后,观察十几小时暂无异常情况出现, ...

随机推荐

  1. JS获取屏幕,浏览器,网页高度宽度

      网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetW ...

  2. ubnt+ros 接入无线

    ubnt+ros 接入无线 一.ubnt配置(UBNT NanoStation LOCOM2 LOCO M2)1.ubnt接poe,转lan接到pc2.ubnt默认ip是192.168.1.20,用户 ...

  3. repeater没有数据显示暂无数据,无记录

    方法就是在FooterTemplate加个Label并根据repeater.Items.Count判断是否有记录.关键代码如下: <FooterTemplate> <asp:Labe ...

  4. MQTT服务器搭建-mosquitto1.4.4安装指南

    Mosquitto mosquitto是一款实现了 MQTT v3.1 协议的开源的消息代理服务软件. 其提供了非常轻量级的消息数据传输协议,采用发布/订阅模式进行工作,可用于物联设备.中间件.APP ...

  5. Sea.js学习4——Sea.js的配置

    可以对 Sea.js 进行配置,让模块编写.开发调试更方便. seajs.config seajs.config(options) 用来进行配置的方法. seajs.config({ // 别名配置 ...

  6. springfox.documentation.spi.DocumentationType配置示例

    Java Code Examples for springfox.documentation.spi.DocumentationType The following are top voted exa ...

  7. python 实现二分法查找

    二分查找图 二叉树: 代码 #!/usr/bin/python #-*-coding:utf-8-*- #----------------------------------------------- ...

  8. 约瑟夫环(Josehpuse)的模拟

    约瑟夫环问题: 0,1,...,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字,求出这个圆圈里剩下的最后一个数字. 这里给出以下几种解法, 1.用队列模拟 每次将前m-1个元 ...

  9. Editable DataGrid 实现列表新增编辑功能

    今天在开发一个功能时候,需要直接在列表实现新增.编辑等功能.于是查看easyui 相关文档,找到相关解决办法. easyui的datagrid支持可编辑功能.它使用户能够向数据网格中添加一个新行.用户 ...

  10. List与字符串转换

    1.将list元素用单引号引起来:List<TransferFocusxfSummaryTop3> topList = getTransferFocusxfSummaryTop3(user ...