165 lines
5.7 KiB
C#
165 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using AllBase;
|
|
using NaiBa.Util.Extension;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Work.CollectClass
|
|
{
|
|
public class PermissionBLL
|
|
{
|
|
public async Task<TData<List<KeyValueBase>>> GetModuleList()
|
|
{
|
|
//http://new.qixiaoke.com/?obj=pinbaobao/nav_group_list&cid=18816&uid=103234&key=936746136&time=1593587666&code=09EC59F354BE1341654C3C7E218931B9
|
|
string url = BaseClass.BuildObjString("nav_group_list");
|
|
string data = UserLogin.GetSignaturex();
|
|
string html = await PostAsync(new HttpParam { Url = url, PostData = data });
|
|
//html = HtmlHelper.UnicodeDecode(html);
|
|
html=GetHtml.UrlDecode(html);
|
|
TData<List<KeyValueBase>> obj = new TData<List<KeyValueBase>>();
|
|
if (html.Equals("null"))
|
|
{
|
|
obj.Tag = 0;
|
|
return obj;
|
|
}
|
|
JObject jObj = JObject.Parse(html);
|
|
if (jObj["list"] != null)
|
|
{
|
|
obj.Result = new List<KeyValueBase>();
|
|
foreach (var item in jObj["list"])
|
|
{
|
|
obj.Result.Add(new KeyValueBase(item["module"].ParseToString(), item["enable"].ParseToString()));
|
|
}
|
|
obj.Tag = 1;
|
|
}
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
public static async Task<string> PostAsync(HttpParam httpParam)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest request = GetHttpWebRequest(httpParam);
|
|
request.Method = "Post";
|
|
request.ServicePoint.Expect100Continue = false;
|
|
byte[] postDataByte = Encoding.UTF8.GetBytes(httpParam.PostData);
|
|
request.ContentLength = postDataByte.Length;
|
|
request.AllowAutoRedirect = false;
|
|
using (Stream requestStream = await request.GetRequestStreamAsync())
|
|
{
|
|
requestStream.Write(postDataByte, 0, postDataByte.Length);
|
|
}
|
|
return await ReadResponseAsync(httpParam, (HttpWebResponse)request.GetResponse());
|
|
}
|
|
catch
|
|
{
|
|
|
|
return string.Empty;
|
|
}
|
|
}
|
|
public static HttpWebRequest GetHttpWebRequest(HttpParam param)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(param.Url);
|
|
if (param.Proxy != null)
|
|
{
|
|
request.Proxy = param.Proxy;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(param.UserAgent))
|
|
{
|
|
request.UserAgent = param.UserAgent;
|
|
}
|
|
else
|
|
{
|
|
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36";
|
|
}
|
|
if (!string.IsNullOrEmpty(param.Accept))
|
|
{
|
|
request.Accept = param.Accept;
|
|
}
|
|
else
|
|
{
|
|
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
|
|
}
|
|
if (string.IsNullOrEmpty(param.AcceptLanguage))
|
|
{
|
|
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
|
|
}
|
|
else
|
|
{
|
|
request.Headers.Add("Accept-Language", param.AcceptLanguage);
|
|
}
|
|
if (!string.IsNullOrEmpty(param.ContentType))
|
|
{
|
|
request.ContentType = param.ContentType;
|
|
}
|
|
else
|
|
{
|
|
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
}
|
|
if (!string.IsNullOrEmpty(param.Referer))
|
|
{
|
|
request.Referer = param.Referer;
|
|
}
|
|
request.Headers.Remove("Cookie");
|
|
if (!string.IsNullOrEmpty(param.Cookie))
|
|
{
|
|
request.Headers.Add("Cookie", param.Cookie);
|
|
}
|
|
if (param.Header != null)
|
|
{
|
|
foreach (KeyValuePair<string, string> header in param.Header)
|
|
{
|
|
request.Headers.Add(header.Key, header.Value);
|
|
}
|
|
}
|
|
if (param.Timeout > 0)
|
|
{
|
|
request.Timeout = param.Timeout;
|
|
}
|
|
else
|
|
{
|
|
request.Timeout = 60 * 1000;
|
|
}
|
|
return request;
|
|
}
|
|
public static async Task<string> ReadResponseAsync(HttpParam httpParam, HttpWebResponse response)
|
|
{
|
|
using (Stream stream = DecompressStream(response))
|
|
{
|
|
using (StreamReader streamReader = new StreamReader(stream, httpParam.ResponseEncoding))
|
|
{
|
|
return await streamReader.ReadToEndAsync();
|
|
}
|
|
}
|
|
}
|
|
public static Stream DecompressStream(HttpWebResponse response)
|
|
{
|
|
Stream responseStream = response.GetResponseStream();
|
|
Stream stream = null;
|
|
if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
{
|
|
stream = new GZipStream(responseStream, CompressionMode.Decompress);
|
|
}
|
|
else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
{
|
|
stream = new DeflateStream(responseStream, CompressionMode.Decompress);
|
|
}
|
|
if (stream == null)
|
|
{
|
|
return responseStream;
|
|
}
|
|
else
|
|
{
|
|
return stream;
|
|
}
|
|
}
|
|
}
|
|
}
|