Http压缩zip,加密base64发送与获取

//数据压缩成Zip再发送

public static string ZipBasebyte(string xml)
{
byte[] bytesToCompress = Encoding.GetEncoding("GBK").GetBytes(xml);
MemoryStream ms = new MemoryStream();
ZipEntry ze = new ZipEntry("servletservice");

using (ZipOutputStream outStream = new ZipOutputStream(ms))
{
outStream.UseZip64 = UseZip64.Off;

outStream.PutNextEntry(ze);
outStream.IsStreamOwner = false;
outStream.Write(bytesToCompress, 0, bytesToCompress.Length);
outStream.Flush();
outStream.CloseEntry();
outStream.Finish();
}
byte[] byteCompress = ms.ToArray();
//把字节转码为Base64格式字符串
string strOut = Convert.ToBase64String(byteCompress);
return strOut;
}

 

/// <summary>
/// 解压缩
/// </summary>
/// <param name="route"></param>
/// <returns></returns>
public static string TUnzipBase64(string route)
{
string outString = string.Empty;
try
{
//先解码Base64格式字符串
byte[] inArr = Convert.FromBase64String(route.Trim());

//从已压缩的字节数组生成原始字节数组
byte[] writeData = new byte[4096];
ZipInputStream InpStream = new ZipInputStream(new MemoryStream(inArr));
ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件

MemoryStream outStream = new MemoryStream();
while (ze != null)//如果解压完ze则是null
{
while (true)
{
int i = InpStream.Read(writeData, 0, writeData.Length);
if (i > 0)
{
outStream.Write(writeData, 0, i);
}
else
{
outStream.Flush();
break;
}
}
ze = InpStream.GetNextEntry();
}
InpStream.Close();
byte[] outArr = outStream.ToArray();
outStream.Close();


outString = Encoding.UTF8.GetString(outArr);
}
catch (NullReferenceException nEx)
{
return nEx.Message;
}
return outString;
}

 

 

//将数据加密为base64

public static string Base64Decode(Encoding encodingType, string result)
{
string encode = string.Empty;
byte[] bytes=encodingType.GetBytes(result);
try
{
encode = Convert.ToBase64String(bytes);
}
catch (Exception err)
{

encode = result;
}
return encode;
}

//获取Http请求

public static string GetRequest(string requestUrl, Dictionary<string, string> headers = null)
{
try
{
int outTime = 6;
using (HttpClient client = new HttpClient())
{
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
client.Timeout = TimeSpan.FromSeconds(outTime);
var responseString = client.GetStringAsync(requestUrl);
string content = responseString.Result;
SystemInfoManager.SaveMessageErr(content);
return content;
}
}
catch (Exception ex)
{
SystemInfoManager.SaveMessageErr(ex.ToString());
return ex.ToString();

}
}

 

//发送Http请求

public static string Restful(string jsonParam,string meth,string url,string userName,string userPassword,string authToken,string contentType,ref string errMsg)
{
string json = string.Empty;
try
{
//创建restful的请求
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = meth;
request.KeepAlive = false;
request.Timeout = 60000;
request.ContentType = contentType;
if (authToken == "") {
request.Accept = "*/*";
}
else
{
request.Accept = "text/plain";
}
request.ServicePoint.Expect100Continue = false;
if (!string.IsNullOrEmpty(userName))
{
request.Headers.Add("userCode", userName);
}
if (!string.IsNullOrEmpty(userPassword))
{
string Password = Base64Decode(Encoding.UTF8, userPassword);
request.Headers.Add("password", Password);
}
if (!string.IsNullOrEmpty(authToken))
{
request.Headers.Add("authToken", authToken);
}
if (!string.IsNullOrEmpty(jsonParam) && meth.ToLower()=="post")
{
string data = ZipBasebyte(jsonParam);
SystemInfoManager.SaveMessage_WCS(data);
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
request.ContentLength = byteData.Length;
using (Stream postStream = request.GetRequestStream())
{
try
{
postStream.Write(byteData, 0, byteData.Length);
}
catch (Exception ex)
{
errMsg = ex.Message;
SystemInfoManager.SaveMessage_WCS("服务器信息报错异常:" + ex.Message);
}
}
}
else
{
request.ContentLength = 0;
}

string[] str = request.Headers.AllKeys;

//接收来自restful的回复
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (!string.IsNullOrEmpty(jsonParam))
{
try
{
if (response.StatusCode == HttpStatusCode.OK)
{
json = "200";
}
}
catch (Exception ex)
{
errMsg = ex.Message;
SystemInfoManager.SaveMessage_WCS("读取服务返回信息报错:" + ex.Message);
}
}
else
{
try
{
//以流的形式读取,返回的就是字符串的json格式
StreamReader reader = new StreamReader(response.GetResponseStream());
json = reader.ReadToEnd();
SystemInfoManager.SaveMessage_WCS("服务返回信息json:" + json);
}
catch (Exception ex)
{
errMsg = ex.Message;
SystemInfoManager.SaveMessage_WCS("读取服务返回信息报错:" + ex.Message);
}
}
}
}
catch (Exception ex)
{
errMsg = ex.Message;
SystemInfoManager.SaveMessage_WCS("与服务器信息交互信息报错异常:" + ex.Message);
}
return json;
}

posted @ 2024-06-15 15:49  KevinSteven  阅读(19)  评论(0编辑  收藏  举报