C#后端调用WebApi地址
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; namespace Services
{
public class HttpHelper<T> where T : class, new()
{
private Uri BaseUri
{
get
{
return new Uri(GlobalString.BaseUrl);
}
} private Uri RequestUri(string requestUrl)
{ return new Uri(BaseUri, requestUrl);
} //用Post还是用Get,要与WebApi的请求方式一致
public async Task<T> GetPostResult(string requestUrl, FormUrlEncodedContent content)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(GlobalString.JsonHeader));
using (var response = await client.PostAsync(RequestUri(requestUrl), content))
{
if (response.IsSuccessStatusCode)
{
var productJsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(productJsonString);
}
return new T();
}
}
}
}
}
服务类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Services
{
public class GlobalString
{
public static string JsonHeader
{
get
{
return "application/json";
}
} public static string BaseUrl
{
get
{
return "http://www.XXX.com";
}
} public static string GetApiUrl
{
get
{
return "apiUrl";
}
} }
}
全局常量类
C#后端调用WebApi地址的更多相关文章
- 后端调用WebApi
using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.W ...
- 跨域调用webapi
web端跨域调用webapi 在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...
- 跨域调用webapi web端跨域调用webapi
web端跨域调用webapi 在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...
- web端跨域调用webapi
在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...
- 跨域学习笔记1--跨域调用webapi
在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...
- web端跨域调用webapi(转)
在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webapi的程序,如下图所示: 由于微软已 ...
- WebApi接口 - 如何在应用中调用webapi接口
很高兴能再次和大家分享webapi接口的相关文章,本篇将要讲解的是如何在应用中调用webapi接口:对于大部分做内部管理系统及类似系统的朋友来说很少会去调用别人的接口,因此可能在这方面存在一些困惑,希 ...
- Web API应用架构在Winform混合框架中的应用(3)--Winfrom界面调用WebAPI的过程分解
最近一直在整合WebAPI.Winform界面.手机短信.微信公众号.企业号等功能,希望把它构建成一个大的应用平台,把我所有的产品线完美连接起来,同时也在探索.攻克更多的技术问题,并抽空写写博客,把相 ...
- WebApi(6) 后台C#调用WebApi
https://www.cnblogs.com/cxd1008/p/6640015.html 今天来写一下后台C#代码如何访问webapi 这里使用HttpClient方法访问webapi也是很常用的 ...
随机推荐
- 我的第一个Socket程序-SuperSocket使用入门(一)
第一次使用Socket,遇到过坑,也涨过姿势,网上关于SuperSocket的教程基本都停留在官方给的简单demo上,实际使用还是会碰到一些问题,所以准备写两篇博客,分别来介绍SuperSocket以 ...
- 关于taskaffinity属性的作用
意味着这activity更喜欢哪个TESK,具体见下方说明 当一个包含FLAG_ACTIVITY_NEW_TASK标志的intent启动一个activity时. 一个新的activity,默认地启动到 ...
- 解决ftp无法连接登录linux的办法
1. 首先安装vsftpd 命令:yum -y install vsftpd 之后开启服务:service vsftpd start 2.关闭防火墙 1) 重启后生效 开启: chkconfig ip ...
- Flow Layout
--------------siwuxie095 将根面板 contentPane 的布局切换为 Flow Layout Flow La ...
- mac上virtualBox的安装和使用
一.下载和安装 去oracle官网下载mac版的virtualBox. 官网下载地址:https://www.virtualbox.org/. 下载好后按照向导进行安装即可. 二.使用方法 1.新建虚 ...
- Tensorflow学习练习-卷积神经网络应用于手写数字数据集训练
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data mn ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
For many programs, developing a mathematical model of running timereduces to the following steps:■De ...
- Comparator 排序
例1: import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import ja ...
- 《Head First Servlets & JSP》-13-过滤器和包装器
过滤器是什么 与servlet非常类似,过滤器就是java组件,请求发送到servlet之前,可以用过滤器截获和处理清求,另外 servlet结束工作之后,在响应发回给客户之前,可以用过滤器处理响应. ...
- [jQuery]使用jQuery.Validate进行客户端验证(初级篇)
以前在做项目的时候就有个很大心病,就是微软的验证控件,虽然微软的验证控件可以帮我们完成大部分的验证,验证也很可靠上手也很容易,但是我就是觉得不爽,主要理由有以下几点: 1.拖控件太麻烦,这个是微软控件 ...