public void Post()
{
//方法一,传json参数
var d = new {
username = " ",
password = " ",
grant_type = "password",
appcode = " ",
companyid = " ",
version = "1.0",
};
var data = JsonConvert.SerializeObject(d);
HttpContent httpContent = new StringContent(data);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (HttpClient httpClient = new HttpClient())
{
string responseJson = httpClient.PostAsync("http://192.168.8.178:1646/token", httpContent)
.Result.Content.ReadAsStringAsync().Result;
} //方法二,传表单参数
FormUrlEncodedContent formContent = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{"username"," " },
{"password"," " },
{"grant_type","password" },
{"appcode"," " },
{"companyid"," " },
{"version","1.0" },
}); using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
httpClient.DefaultRequestHeaders.Add("Accept", @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
HttpResponseMessage response = httpClient.PostAsync("http://192.168.8.178:1646/token", formContent).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
}
} ///方法三,传字节流
using (HttpClient http = new HttpClient())
{
http.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
http.DefaultRequestHeaders.Add("Accept", @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); HttpResponseMessage message = null; string xx = @"{ ""username"":"" "",""password"":"" "",""grant_type"":""password"",""appcode"":"" "",""companyid"":"" "",""version"":""1.0""}";
using (Stream dataStream = new MemoryStream(Encoding.Unicode.GetBytes(xx) ?? new byte[]))
{
using (HttpContent content = new StreamContent(dataStream))
{
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var task = http.PostAsync("http://192.168.8.178:1646/token", content);
message = task.Result;
}
}
if (message != null && message.StatusCode == System.Net.HttpStatusCode.OK)
{
using (message)
{
string result = message.Content.ReadAsStringAsync().Result;
}
}
} }

HttpClient 调用WebAPI时,传参的三种方式的更多相关文章

  1. 【service调用dao层传参的三种方式】

    第一种方案:默认数组角标: service Public User selectUser(String name,String area); mapper: <select id="s ...

  2. react-绑定this并传参的三种方式

    绑定this并传参的三种方式 在事件中绑定this并传参: <input type="button" value="在事件中绑定this并传参" onCl ...

  3. vue路由传参的三种方式以及解决vue路由传参页面刷新参数丢失问题

    最近项目中涉及到跨页面传参数和后台进行数据交互,看到需求之后第一反应就是用路由传参来解决:Vue中给我们提供了三种路由传参方式,下面我们一个一个的来看一下: 方法一:params传参: this.$r ...

  4. React中使用 react-router-dom 路由传参的三种方式详解【含V5.x、V6.x】!!!

    路由传值的三种方式(v5.x) params参数 //路由链接(携带参数): <Link to='/demo/test/tom/18'}>详情</Link> //或 <L ...

  5. Vue-router路由传参的三种方式

    本文简单介绍下三种路由传参: (1)在路由中配置 { path : ‘/home/:id’, name : ‘Dome’, component } 然后写调用的时候 this.$router.push ...

  6. vue路由传参的三种方式区别(params,query)

    最近在做一个项目涉及到列表到详情页的参数的传递,网上搜索一下路由传参,结合自己的写法找到一种适合自己的,不过也对三种写法都有了了解,在此记录一下 <ul class="table_in ...

  7. vue里面路由传参的三种方式

    1.方式一 通过query的方式也就是?的方式路径会显示传递的参数 HTML的方式<router-link :to="{name:xxx,query:{page:1,code:8899 ...

  8. vue路由传参的三种方式

    方式一 通过query方式传参 这种情况下 query传递的参数会显示在url后面 this.$router.push({ path: '/detail', query: { id: id } }) ...

  9. 3.struts2接收页面传参的三种方式

    Struts2通过拦截器机制封装了三种接收页面参数的方式: 1.属性驱动 2.模型驱动(有两种) Domain ModelDriven 1.属性驱动:这种方式比较简单,只要你直接在页面定义变量并且符合 ...

随机推荐

  1. webDriver基本运用

    import time from selenium import webdriver import unittest import HTMLTestRunner # webDriver基本应用 cla ...

  2. chrome启动参数之

    --remote-debugging-port Chrome 启动的时候,默认是关闭了调试端口的,如果要对一个目标 Chrome PC 浏览器进行调试,那么启动的时候,可以通过传递参数来开启 Chro ...

  3. 2018-2019-2 网络对抗技术 20165206 Exp6 信息搜集与漏洞扫描

    - 2018-2019-2 网络对抗技术 20165206 Exp6 信息搜集与漏洞扫描 - 实验任务 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口 ...

  4. python中thread的setDaemon、join的用法的代码

    下面内容是关于python中thread的setDaemon.join的用法的内容. #! /usr/bin/env python import threading import time class ...

  5. Django by example -----1总结

    根据django by example 完成了第一个例子,总结如下. 第一: django的orm真的很方便,避免了sql语句的使用,你所需要的,django基本都已经封装好了,一些字段很有意思. 第 ...

  6. MyCat读写分离-笔记(四)

    概述 Mycat能够实现数据库读写分离,不能实现主从同步,数据库的备份还是基于数据库层面的.Mycat只是数据库的中间件: Mycat读写分离配置 在MySQL中间件出现之前,对于MySQL主从集群, ...

  7. pkuwc2018题解

    题解: 思路挺好想的..然而今天写代码写成傻逼了 d1t1: 首先比较暴力的就是$f[i][j]$表示i个这个点是j的概率 然后前缀和一下dp就是$n^2$的 部分分树形态随机就说明树深度是$log$ ...

  8. postgresql drop表后空间不释放

    数据库执行drop table XXX后,查看du -sh 已下降,但df -h /pgdb没有减少,是有还有进程使用对应的文件句柄 1.通过表名确认文件ID select pg_relation_f ...

  9. java中的时间类型

    java时间的格式 SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Stri ...

  10. 网络对抗技术 20165220 Exp3 免杀原理与实践

    实验任务 1 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程等免杀工具或技巧: 2 通过组合应用各种技术实现恶意代码免杀(1 ...