public class BaseHttpClient
{
protected string contentType; public BaseHttpClient()
{
this.contentType = "application/json";
} protected const int RESPONSE_OK = ;
//设置读取超时时间
private const int DEFAULT_SOCKET_TIMEOUT = ( * ); // milliseconds /// <summary>
/// HTTP 验证
/// </summary>
/// <returns></returns>
public virtual Dictionary<string, string> Authorization()
{
return null;
} /// <summary>
/// 构建请求参数
/// </summary>
/// <param name="dicList"></param>
/// <returns></returns>
public String BuildQueryStr(Dictionary<String, String> dicList)
{
String postStr = dicList.Aggregate("", (current, item) => current + item.Key + "=" + HttpUtility.UrlEncode(item.Value, Encoding.UTF8) + "&"); postStr = postStr.Substring(, postStr.LastIndexOf('&'));
return postStr;
} /// <summary>
/// 发送请求
/// </summary>
/// <param name="method">请求方式</param>
/// <param name="url">请求链接</param>
/// <param name="reqParams">请求参数</param>
/// <returns></returns>
public ResultDTO SendRequest(Method method, String url, String reqParams)
{
HttpWebRequest myReq = null;
HttpWebResponse response = null;
try
{
if (method == Method.Get||method==Method.Delete)
{
url += "?" + reqParams;
}
myReq = (HttpWebRequest) WebRequest.Create(url);
myReq.Method = method.ToString();
myReq.ReadWriteTimeout = DEFAULT_SOCKET_TIMEOUT;
myReq.ContentType = contentType; //权限验证
var auth = this.Authorization();
if (auth != null)
{
foreach (var item in auth)
{
myReq.Headers.Add(item.Key, item.Value);
}
} if (myReq.Method == "POST" || myReq.Method == "Put")
{
byte[] bs = Encoding.UTF8.GetBytes(reqParams);
myReq.ContentLength = bs.Length;
using (Stream reqStream = myReq.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
}
response = (HttpWebResponse) myReq.GetResponse();
if (Equals(response.StatusCode, HttpStatusCode.OK) ||
Equals(response.StatusCode, HttpStatusCode.Created))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return WebApi.Success(reader.ReadToEnd());
}
}
return WebApi.Error("");
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
//HttpStatusCode errorCode = ((HttpWebResponse) e.Response).StatusCode;
//string statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
using (StreamReader sr = new StreamReader(((HttpWebResponse) e.Response).GetResponseStream(),
Encoding.UTF8))
{
return WebApi.Error(sr.ReadToEnd());
}
}
return WebApi.Error(e.Message);
}
//这里不再抓取非http的异常,如果异常抛出交给开发者自行处理
//catch (System.Exception ex)
//{
// String errorMsg = ex.Message;
// Debug.Print(errorMsg);
//}
finally
{
if (response != null)
{
response.Close();
}
if (myReq != null)
{
myReq.Abort();
}
}
}
} //请求方式
public enum Method
{
Post,
Delete,
Get,
Put
}

C#关于HttpClient的统一配置(一)的更多相关文章

  1. 基于ZK构建统一配置中心的方案和实践

    背景: 近期使用Zk实现了一个简单的配置管理的小东西,在此开源出来,有兴趣的希望提出您的宝贵意见.如果恰巧您也使用或者接触过类似的东西, 也希望您可以分享下您觉得现在这个项目可以优化和改进的地方. 项 ...

  2. Android 100多个Styles快速开发布局XML,一行搞定View属性,一键统一配置UI...

    Android开发中大量使用XML代码作为界面的布局,使用styles能大幅精简XML代码. 比如下面这个界面从AlertDialog至PlacePickerWindow有19个样式相同的跳转Item ...

  3. JBOSS EAP 6 系列五 Managed domains 管理域最主要的功能是“统一部署,统一配置”

    摘要 本文首先介绍Managed Domain的概念,管理域最主要的功能是"统一部署,统一配置".接下来通过一个实例在"统一配置"部分实现一个双机配置起来的域, ...

  4. .NET Core微服务之基于Apollo实现统一配置中心

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.关于统一配置中心与Apollo 在微服务架构环境中,项目中配置文件比较繁杂,而且不同环境的不同配置修改相对频繁,每次发布都需要对应修改 ...

  5. springcloud-spring cloud config统一配置中心

    统一配置中心 为什么需要统一配置中心? 统一配置中心顾名思义,就是将配置统一管理,配置统一管理的好处是在日后大规模集群部署服务应用时相同的服务配置一致,日后再修改配置只需要统一修改全部同步,不需要一个 ...

  6. 多个SpringMVC项目配置统一管理(来自于springCloud的统一配置思路)

    因公司项目分多个系统进行开发,而系统架构几乎完全一样,所以同样的配置文件会存在不同的系统中 当其中的某些配置需要修改时,就需要依次把所有系统中相关的配置都修改掉 纯耗时且没技术含量的体力活 所以借鉴S ...

  7. 基于Apollo实现.NET Core微服务统一配置(测试环境-单机)

    一.前言 注:此篇只是为测试环境下的快速入门.后续会给大家带来生产环境下得实战开发. 具体的大家可以去看官方推荐.非常的简单明了.以下介绍引用官方内容: Apollo(阿波罗)是携程框架部门研发的分布 ...

  8. Android多个Module统一配置相同jar或库的版本号

    Android Studio多个Module依赖相同的库时对版本号进行统一配置 在Android项目中,一个项目经常会依赖其他的一个甚至多个库文件,在这种依赖的时候最常见的一个错误就是 jar包版本不 ...

  9. 携程框架Apollo实现.NET Core微服务统一配置(测试环境-单机)

    Apollo实现.NET Core微服务统一配置(测试环境-单机) https://www.cnblogs.com/guolianyu/p/10065999.html 一.前言 注:此篇只是为测试环境 ...

随机推荐

  1. [LeetCode217]Contains Duplicate

    题目:Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  2. UVA 11769 All Souls Night 的三维凸包要求的表面面积

    主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2869">点击打开链接 求给定的 ...

  3. 什么是PV,UV。

    PV浏览(Page View).该网页访问量,每次页面打开PV统计+1,也刷新. IP接入号码指独立IP接入号码,计算基于独立IP在计算的时间段来计算访问我们的网站1二级IP接入号码. 是否这个计算在 ...

  4. C#多线程编程实例 螺纹与窗口交互

    C#多线程编程实例 螺纹与窗口交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = new Thread ...

  5. Web Deploy发布网站及常见问题解决方法(图文)

    Web Deploy发布网站及常见问题解决方法(图文) Windows2008R2+IIs7.5 +Web Deploy 3.5 Web Deploy 3.5下载安装 http://www.iis.n ...

  6. Palindromes&nbsp;_easy&nbsp;version

    Time Limit: 1Sec  MemoryLimit: 64 MB Submit:165  Solved: 76 [Submit][Status][WebBoard] Description & ...

  7. poj1276--Cash Machine(多背包被判刑了)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27804   Accepted: 9915 Des ...

  8. Block学习一门:基本使用,使用block包NSURLRequest异步请求

    首先,看一下下面的代码: void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"== ...

  9. [创意标题] spoj 11354 Amusing numbers

    意甲冠军: 给k(1<=k<=10^15),先询问k 大只包含数字5和6的数目是多少 实例 1那是,5 ,3那是,55 .4那是,56 思考: 首先,我们可以找到.有许多2这是头号,有两个 ...

  10. combobox自己主动提示组件加入无选中项清空功能

    这个标题非常绕口,只是这也是想了半天的成果,对不起体育老师了. 标题想表达的是:之前讲过的用combobox实现自己主动提示组件.只是如今规定该组件不能够保存data中不存在的数据. 最初的想法是通过 ...