public class Httphelper
{
public static string Post1(string url, string postString)
{ using (WebClient webClient = new WebClient())
{
byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,
webClient.Headers.Add("Content-Type", "application/json");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
string srcString = Encoding.UTF8.GetString(responseData);//解码
return srcString;
}
} public static string Post(string url, string postData)
{
string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length;
//req.Timeout = 30000;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, , data.Length); reqStream.Close();
} HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //获取响应内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
} return result;
}
}

HttpClient 调用

  /// <summary>
/// post 同步
/// </summary>
/// <param name="url"></param>
/// <param name="postString"></param>
/// <returns></returns>
public static string Post(string url, string postData)
{
try
{
using (HttpClient _httpClient = new HttpClient())
{
StringContent httpClient = new StringContent(postData, Encoding.UTF8, "application/json");
var response = _httpClient.PostAsync(url, httpClient).Result;
string jsonstr = response.Content.ReadAsStringAsync().Result;
return jsonstr;
}
}
catch (Exception ex)
{
_logger.LogError(string.Format("Api调用异常--url【{0}】,参数【{1}】", url, postData), ex);
return "";
}
}
/// <summary>
/// post 异步
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <returns></returns>
public static async void PostAsync(string url, string postData)
{
try
{
using (HttpClient _httpClient = new HttpClient())
{
StringContent httpClient = new StringContent(postData, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(url, httpClient);
string jsonstr = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
_logger.LogError(string.Format("Api调用异常--url【{0}】,参数【{1}】", url, postData), ex);
}
}

cs端调用webApi的更多相关文章

  1. cs端调用Ajax

    private static string Descoder() { //ajax地址 string MealFilePath = "http://***/user/SetWebsite.a ...

  2. 跨域调用webapi web端跨域调用webapi

    web端跨域调用webapi   在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...

  3. web端跨域调用webapi

    在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...

  4. web端跨域调用webapi(转)

    在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...

  5. WebApi接口 - 如何在应用中调用webapi接口

    很高兴能再次和大家分享webapi接口的相关文章,本篇将要讲解的是如何在应用中调用webapi接口:对于大部分做内部管理系统及类似系统的朋友来说很少会去调用别人的接口,因此可能在这方面存在一些困惑,希 ...

  6. 跨域调用webapi

    web端跨域调用webapi   在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...

  7. 跨域学习笔记1--跨域调用webapi

    在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...

  8. 用C#基于WCF创建TCP的Service供Client端调用

    本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...

  9. .NET Core 下调用WebAPI

    前言 今天我们介绍多种客户端调用WebApi的方式,可以是原生写的,也可以借助.NET 框架下的其他HTTP库.我们一起来看看它们之间的一些异同吧- RestSharp 首先要介绍的就是这款REST ...

随机推荐

  1. 【洛谷九月月赛T1】签到题(bsgs)(快速乘)

    说好的签到题呢qwq....怎么我签到题都不会啊qwq 之后看了bsgs才发现貌似不是那么那么难fake!!什么东西... 先贴上部分分做法(也就是枚举1的个数,然后每一步都进行取模(这和最后取模结果 ...

  2. django学习篇

      https://www.cnblogs.com/alex3714/category/818260.html https://www.cnblogs.com/zhanghongfeng/catego ...

  3. python获取IP位置来源

    import requests import IPy def get_location(ip): url = 'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2D ...

  4. Linux下iptables防火墙用法规则详解

    管理网络流量是系统管理员必需处理的最棘手工作之一,我们必需规定连接系统的用户满足防火墙的传入和传出要求,以最大限度保证系统免受×××.很多用户把 Linux 中的iptables当成一个防火墙,从严格 ...

  5. Setup "EQGRP_Lost_in_Translation" Of NAS

    Setup "EQGRP_Lost_in_Translation" Of NAS 1.前言: 北京时间4月14号晚,TheShadowBrokers在steemit.com博客上放 ...

  6. 【算法】C++用链表实现一个箱子排序附源代码详解

    01 箱子排序 1.1 什么是分配排序? 分配排序的基本思想:排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O ...

  7. jenkins配置以cygwin环境的子节点

    1.为Cygwin配置上sshd服务 ⑴.双击点击如下安装文件,并一直选取下一步 ⑵.到这个步骤停止,并且在search栏里输入openssh,并下载搜出的两个程序 ⑶.下载好以后以管理员权限打开cy ...

  8. 在使用Vue搭建前端服务器时,路由出现#号的解决办法

    路由模式解析 这里要讲vue-router的路由模式,首先要了解的一点就是路由是由多个URL组成的,使用不同的URL可以相应的导航到不同的位置.如果有进行过服务器开发或者对http协议有所了解就会知道 ...

  9. 【Python】子域名查询脚本

    脚本学习,多写写就会啦,来一发个人编写的超级无敌low的子域名查询脚本 #coding:utf-8 import re import requests import urllib import url ...

  10. vue中实现国际化--语言切换(转载)

    https://segmentfault.com/a/1190000011800593