admin
2020-06-13 5dde755029a86cf8e7052f4878e7c64b94715c38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
{
 
    delegate void OnSuccess(String result);
 
    class RequestParams {
      public   String url;
        public Dictionary<String, String> paramsMap;
        public Dictionary<String, String> headers;
        public OnRequestResultListener listener;
        public OnSuccess success;
        public RequestParams(String url, Dictionary<String, String> paramsMap, Dictionary<String, String> headers, OnRequestResultListener listener) {
            this.url = url;
            this.paramsMap = paramsMap;
            this.headers = headers;
            this.listener = listener;
        }
 
        public RequestParams(String url, Dictionary<String, String> paramsMap, Dictionary<String, String> headers, OnSuccess listener)
        {
            this.url = url;
            this.paramsMap = paramsMap;
            this.headers = headers;
            this.success = 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);
                if (param.listener!=null)
                    param.listener.onSuccess(200, result);
                if (param.success != null)
                    param.success(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<String, String> paramsMap = new Dictionary<String, String>();
            paramsMap.Add("baseUrl", baseUrl);
            Dictionary<String, String> headers = new Dictionary<String, String>();
            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<String, String> headers = new Dictionary<String, String>();
            headers.Add("token", token);
            Dictionary<String, String> paramsMap = new Dictionary<String, String>();
            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<String, String> headers = new Dictionary<String, String>();
            headers.Add("token", token);
            Dictionary<String, String> paramsMap = new Dictionary<String, String>();
            paramsMap.Add("content", content);
            startRequest(new RequestParams(BASE_URL + "parse/uploadContent", paramsMap, headers, listener));
        }
 
 
        //搜索商品
        public static void searchGoods(String token, SearchInfo searchInfo,int page, OnSuccess listener) { 
            Dictionary<String, String> headers = new Dictionary<String, String>();
            headers.Add("token", token);
            Dictionary<String, String> paramsMap = new Dictionary<String, String>();
            paramsMap.Add("searchInfo", JsonConvert.SerializeObject(searchInfo));
            paramsMap.Add("page",page+"");
            startRequest(new RequestParams(BASE_URL + "sdlj/goods/searchGoods", paramsMap, headers, listener));
        }
    }
}