using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using WindowsFormsApp1.entity; namespace WindowsFormsApp1.utils { class RequestParams { public String url; public Dictionary paramsMap; public Dictionary headers; public OnRequestResultListener listener; public RequestParams(String url, Dictionary paramsMap, Dictionary headers, OnRequestResultListener listener) { this.url = url; this.paramsMap = paramsMap; this.headers = headers; this.listener = listener; } } class ApiUtil { static String BASE_URL = "http://192.168.1.122:8082/"; static void basePost(object obj) { RequestParams param = (RequestParams)obj; try { if (param.listener != null) param.listener.onStart(); string result = HttpUtil.HttpPost(param.url, param.paramsMap, param.headers); param.listener.onSuccess(200, result); } catch (Exception e) { Console.WriteLine("出错:" + e.Message); } finally { if (param.listener != null) param.listener.onFinish(); } } private static void startRequest(RequestParams param) { Thread thread = new Thread(basePost); thread.Start(param); } public static void getLoginUrl(OnRequestResultListener listener) { startRequest(new RequestParams(BASE_URL + "parse/getLoginUrl", null, null, listener)); } public static void getRequestUrl(String token, String baseUrl, OnRequestResultListener listener) { Dictionary paramsMap = new Dictionary(); paramsMap.Add("baseUrl", baseUrl); Dictionary headers = new Dictionary(); headers.Add("token", token); startRequest(new RequestParams(BASE_URL + "parse/getRequestUrl", paramsMap, null, listener)); } public static void uploadCookie(String token, String cookies, OnRequestResultListener listener) { Dictionary headers = new Dictionary(); headers.Add("token", token); Dictionary paramsMap = new Dictionary(); paramsMap.Add("cookies", cookies); startRequest(new RequestParams(BASE_URL + "parse/uploadCookies", paramsMap, headers, listener)); } public static void uploadContent(String token, String content, OnRequestResultListener listener) { Dictionary headers = new Dictionary(); headers.Add("token", token); Dictionary paramsMap = new Dictionary(); paramsMap.Add("content", content); startRequest(new RequestParams(BASE_URL + "parse/uploadContent", paramsMap, headers, listener)); } //搜索商品 public static List searchGoods(String token, SearchInfo searchInfo,int page, OnRequestResultListener listener) { Dictionary headers = new Dictionary(); headers.Add("token", token); Dictionary paramsMap = new Dictionary(); paramsMap.Add("searchInfo", JsonConvert.SerializeObject(searchInfo)); startRequest(new RequestParams(BASE_URL + "parse/uploadCookies", paramsMap, headers, listener)); return null; } } }