索引:
一、说明
1) 这个类 是我 在真实项目中,优化解决真实问题 时,不参考第三方代码,完全由自己查阅MSDN官方文档 , 完成的一个真实生产环境中使用的功能类
2) 读者在使用此类时,请尊重原创,在代码中加上原创注释:// Author -- Meng.NET (cnblogs.com) ,同时欢迎 二次改进、二次创作 以共同进步
3) 此代码以【面向对象】、【C#闭包】、【异步回调】、【超时】、【等待】、【自动重试】方式实现及完成,且可以配置扩展
二、代码
废话不多说,上干货,代码如下:
/// <summary>
/// 异步 Http
/// </summary>
public class Remoter
{
/*
* LM,2016/08/18
* C#闭包化,异步化,Web操作
* 以便支持POS多接口多操作同时使用
*/ /// <summary>
/// 请求地址
/// </summary>
public string URL { get; set; } /// <summary>
/// 请求方式
/// </summary>
public string RequestMethod { get; set; } /// <summary>
/// 请求数据
/// </summary>
public string JsonContent { get; set; } //
private byte[] Buffer { get; set; }
private Stream RequestStream { get; set; }
private HttpWebRequest Request { get; set; }
private bool ResponseFlag { get; set; }
private string Result { get; set; }
private bool TimeoutFlag { get; set; }
private int TimeoutTime { get; set; }
private bool RetryFlag { get; set; }
private int RetryCount { get; set; }
private int WaitSleep { get; set; }
private int TrySleep { get; set; } // 初始化
public Remoter()
{
//
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true); //
this.URL = string.Empty;
this.Request = default(HttpWebRequest);
this.JsonContent = string.Empty;
this.Buffer = default(byte[]);
this.RequestStream = default(Stream);
this.ResponseFlag = false;
this.Result = string.Empty;
this.TimeoutFlag = false;
this.TimeoutTime = * ;
this.RetryFlag = false;
this.RetryCount = ;
this.WaitSleep = ;
this.RequestMethod = "POST";
this.TrySleep = ;
} /// <summary>
/// 获取响应数据
/// </summary>
public string GetRemoteData()
{
//
if(string.IsNullOrWhiteSpace(this.URL))
{
throw new Exception("HttpAsync.URL,未赋值!");
} //
RemoteNew(SetResult); //
var timeNum = ;
while (true)
{
if (ResponseFlag)
{
break;
}
if (TimeoutFlag)
{
throw new Exception(string.Format("请求超时!超时时间:{0}S", TimeoutTime / ));
}
timeNum += WaitSleep;
if (timeNum >= TimeoutTime)
{
TimeoutFlag = true;
}
Thread.Sleep(WaitSleep);
} //
return Result;
} //
private void RemoteNew(Action<Remoter, string> action)
{
//
var reNum = ;
for (var i = ; i < this.RetryCount; i++)
{
try
{
//
var uri = URL; //
this.Request = WebRequest.Create(uri) as HttpWebRequest;
this.Request.KeepAlive = false;
this.Request.Method = this.RequestMethod;
this.Request.Credentials = CredentialCache.DefaultCredentials;
if (this.RequestMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
{
this.Buffer = Encoding.UTF8.GetBytes(this.JsonContent);
this.Request.ContentLength = this.Buffer.Length;
this.Request.ContentType = "application/json";
this.RequestStream = this.Request.GetRequestStream();
this.RequestStream.Write(this.Buffer, , this.Buffer.Length);
this.RequestStream.Close();
} //
this.Request.BeginGetResponse((arr) =>
{
//
var state = arr.AsyncState as Remoter;
//
var response = state.Request.EndGetResponse(arr) as HttpWebResponse;
var respStream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
action(state, respStream.ReadToEnd());
respStream.Close();
response.Close();
}, this);
//
break;
}
catch (Exception ex)
{
Thread.Sleep(this.TrySleep);
reNum++;
if (reNum == this.RetryCount)
{
throw new Exception(string.Format("重试失败!重试次数:{0}次,失败原因:{1}", this.RetryCount, ex.Message));
}
continue;
}
}
}
private void SetResult(Remoter state, string jsonData)
{
if (!string.IsNullOrWhiteSpace(jsonData))
{
state.Result = jsonData;
state.ResponseFlag = true;
}
}
}
Remoter.cs
使用方式:
GET:
var remoter = new Remoter();
remoter.RequestMethod = "GET";
remoter.URL = "这里是你要请求的URL";
var response = remoter.GetRemoteData();
POST:
var remoter = new Remoter();
remoter.RequestMethod = "POST";
remoter.URL = "你要请求的URL";
remoter.JsonContent = "你要想URL发送的JSON数据";
var response = remoter.GetRemoteData();
三、代码解析
public Remoter() 初始化本类,可配置到 App.config/Web.config 中
//
this.URL = string.Empty;
this.Request = default(HttpWebRequest);
this.JsonContent = string.Empty;
this.Buffer = default(byte[]);
this.RequestStream = default(Stream);
this.ResponseFlag = false;
this.Result = string.Empty;
this.TimeoutFlag = false;
this.TimeoutTime = * ;
this.RetryFlag = false;
this.RetryCount = ;
this.WaitSleep = ;
this.RequestMethod = "POST";
this.TrySleep = ;
可从配置文件读取部分
public string URL 要请求的地址
public string RequestMethod HTTP 动词
public string JsonContent POST 请求时,发送的json数据
private byte[] Buffer 设置请求流的byte数组
private Stream RequestStream 请求流
private HttpWebRequest Request HTTP请求对象
private bool ResponseFlag 与请求对应的响应是否成功标识
private string Result 回调状态保持对象,保存响应结果用
private bool TimeoutFlag 总超时时间是否超时标识
private int TimeoutTime 总超时时间(包含若干次重试),默认10s
private int RetryCount 总URL调用(失败)自动重试次数,默认3次
private int WaitSleep 主线程,仿Ajax回调等待时间,默认10ms
private int TrySleep 每次请求地址失败后,重试时间间隔,默认2s
public string GetRemoteData() 解析:
//
var timeNum = ;
while (true)
{
if (ResponseFlag)
{
break;
}
if (TimeoutFlag)
{
throw new Exception(string.Format("请求超时!超时时间:{0}S", TimeoutTime / ));
}
timeNum += WaitSleep;
if (timeNum >= TimeoutTime)
{
TimeoutFlag = true;
}
Thread.Sleep(WaitSleep);
}
主线程,仿Ajax回调等待
private void RemoteNew(Action<Remoter, string> action) 解析:
private void RemoteNew(Action<Remoter, string> action)
{
//
var reNum = ;
for (var i = ; i < this.RetryCount; i++)
{
try
{
// 此处省略
//... ... //
break;
}
catch (Exception ex)
{
Thread.Sleep(this.TrySleep);
reNum++;
if (reNum == this.RetryCount)
{
throw new Exception(string.Format("重试失败!重试次数:{0}次,失败原因:{1}", this.RetryCount, ex.Message));
}
continue;
}
}
}
调用URL失败,自动重试机制
//
this.Request.BeginGetResponse((arr) =>
{
//
var state = arr.AsyncState as Remoter;
//
var response = state.Request.EndGetResponse(arr) as HttpWebResponse;
var respStream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
action(state, respStream.ReadToEnd());
respStream.Close();
response.Close();
}, this);
异步响应回调,闭包与自状态保持
private void SetResult(Remoter state, string jsonData) 解析:
state.Result = jsonData;
state.ResponseFlag = true;
异步响应成功后,在状态保持中赋值结果
四、计划中的开源项目...
计划后续会将生产中解决问题的诸多地方汇集成一个项目【Meng.Net.dll】并开源至GitHub上,欢迎交流,共同提高~~
至于本文所讲的类会在哪个命名空间中,还没想好,项目的整体结构及主打方向,敬请期待......
蒙
2016-09-24 22:37 周六