Commit 4c8c0057 authored by 黄奎's avatar 黄奎

1111

parent 3ff13040
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Test.Helper
{
/// <summary>
/// 会麦
/// </summary>
public class HuiMaiHelper
{
static string ApiUrl = "https://oc.huimaihuishou.com";
static List<string> tempArray = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
static string cookie = "PHPSESSID=6ou76sh2d4ccceleic177iojo6; expires=Fri, 22-Jul-2022 18:18:51 GMT; Max-Age=54000; path=/";
public static void GetCategoryList()
{
string url = ApiUrl + "/apiv1/Category/get_category_list";
string jsonData = HttpGet(url, cookie);
var list = ParseCategoryList(jsonData);
List<brandItem> brandList = new List<brandItem>();
foreach (var item in list)
{
GetBrandList(item, brandList);
}
string str = "";
}
private static List<cateGoryListItem> ParseCategoryList(string json)
{
List<cateGoryListItem> list = new List<cateGoryListItem>();
if (!string.IsNullOrEmpty(json))
{
JObject rootObj = JObject.Parse(json);
if (rootObj!=null&&!string.IsNullOrEmpty(rootObj["result"].ToString()))
{
JObject resultObj = JObject.Parse(rootObj["result"].ToString());
if (resultObj != null && !string.IsNullOrEmpty(resultObj["data"].ToString()))
{
JObject dataObj = JObject.Parse(resultObj["data"].ToString());
if (dataObj != null && !string.IsNullOrEmpty(dataObj["list"].ToString()))
{
var tempList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<cateGoryListItem>>(dataObj["list"].ToString());
if (tempList != null && tempList.Count > 0)
{
list.AddRange(tempList);
}
}
}
}
}
return list;
}
/// <summary>
/// 获取商品详情
/// </summary>
/// <returns></returns>
public static void GetBrandList(cateGoryListItem rootItem, List<brandItem> brandList)
{
string newImaApi = ApiUrl + "/apiv1/Brand/get_brand_list_by_category";
var data = new
{
category_id = rootItem.id
};
string jsonData = HttpPost(newImaApi, cookie, Newtonsoft.Json.JsonConvert.SerializeObject(data));
var list = ParseCategory(jsonData);
if (list != null && list.Count > 0)
{
foreach (var item in list)
{
GetBrandInfo(item);
}
}
if (list != null && list.Count > 0)
{
brandList.AddRange(list);
}
}
/// <summary>
/// 获取分类详情
/// </summary>
/// <param name="item"></param>
public static void GetBrandInfo(brandItem item)
{
string newImaApi = ApiUrl + "/apiv1/Brand/get_brand_img";
var data = new
{
category_id = item.category_id,
brand_id = item.id
};
var postMsg = new
{
data
};
string jsonData = HttpPost(newImaApi, cookie, Newtonsoft.Json.JsonConvert.SerializeObject(postMsg));
if (!string.IsNullOrEmpty(jsonData))
{
JObject rootObj = JObject.Parse(jsonData);
if (rootObj != null && !string.IsNullOrEmpty(rootObj["data"].ToString()))
{
JObject resultObj = JObject.Parse(rootObj["data"].ToString());
if (resultObj != null)
{
JArray imgArray = JArray.Parse(resultObj["img_array"].ToString());
string upload_remark = resultObj["upload_remark"].ToString();
string remark = resultObj["remark"].ToString();
string bottom_remark = resultObj["bottom_remark"].ToString();
}
}
}
}
public static List<brandItem> ParseCategory(string jsonData)
{
List<brandItem> list = new List<brandItem>();
if (!string.IsNullOrEmpty(jsonData))
{
JObject jobj = JObject.Parse(jsonData);
if (jobj != null && !string.IsNullOrEmpty(jobj["result"].ToString()))
{
JObject subObj = JObject.Parse(jobj["result"].ToString());
if (subObj != null && subObj["code"].ToString() == "1")
{
JObject dataObj = JObject.Parse(subObj["data"].ToString());
var hotListStr = dataObj["hot_list"].ToString();
var listStr = dataObj["list"].ToString();
if (!string.IsNullOrEmpty(hotListStr))
{
var hotList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<brandItem>>(hotListStr);
if (hotList != null && hotList.Count > 0)
{
list.AddRange(hotList);
}
}
if (!string.IsNullOrEmpty(listStr))
{
JObject lObj = JObject.Parse(listStr);
foreach (var item in tempArray)
{
if (lObj[item] != null)
{
string str = lObj[item].ToString();
if (!string.IsNullOrEmpty(str))
{
var dataList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<brandItem>>(str);
if (dataList != null && dataList.Count > 0)
{
list.AddRange(dataList);
}
}
}
}
}
}
}
}
return list;
}
/// <summary>
/// Get获取数据
/// </summary>
/// <param name="url">url地址</param>
/// <param name="encode">编码方式</param>
/// <param name="Source">来源</param>
/// <returns></returns>
static string HttpGet(string url, string cookie)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Headers.Add("Cookie", cookie);
myRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
reader.Close();
return content;
}
/// <summary>
/// Post提交数据
/// </summary>
/// <param name="url">url地址</param>
/// <param name="body">参数</param>
/// <param name="contenttype">参数</param>
/// <returns></returns>
public static string HttpPost(string url,string cookie, string body)
{
try
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Cookie", cookie);
request.Method = "POST";
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
request.Timeout = 600000;// 600秒 超时时长
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
var res = (HttpWebResponse)ex.Response;
StringBuilder sb = new StringBuilder();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
sb.Append(sr.ReadToEnd());
throw new Exception(sb.ToString());
}
}
}
/// <summary>
/// 分类列表
/// </summary>
public class cateGoryListItem
{
public int id { get; set; }
public string category_name { get; set; }
public string name { get; set; }
public string logo { get; set; }
}
public class brandItem
{
public int id { get; set; }
public string brand_name { get; set; }
public int parent_id { get; set; }
public string logo { get; set; }
public int category_id { get; set; }
public string initial { get; set; }
public int is_head_character { get; set; }
public string head_character_url { get; set; }
public string brand_name_alpha { get; set; }
public string brand_name_title { get; set; }
public string name { get; set; }
}
}
......@@ -13,7 +13,7 @@ namespace Test
static void Main(string[] args)
{
Console.WriteLine("Start......");
//Helper.FreightRulesHelper.GetData();
Helper.HuiMaiHelper.GetCategoryList();
//Test();
Console.WriteLine("End......");
......
......@@ -70,6 +70,7 @@
<Compile Include="DBHelper\MySqlHelper.cs" />
<Compile Include="Helper\FreightRulesHelper.cs" />
<Compile Include="Helper\GoodsHelper.cs" />
<Compile Include="Helper\HuiMaiHelper.cs" />
<Compile Include="Model\Destination.cs" />
<Compile Include="Model\FreightRules.cs" />
<Compile Include="Helper\UserHelper.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment