bingbo
2023-11-26 948b8191b8f32ce002b3f418b3669e01f745bcaa
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.thhy.screen.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.thhy.general.common.BasicMessage;
import com.thhy.general.exception.BasicException;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
 
public class HttpUtils {
 
    private static String BaseHost = "http://58.210.64.90:9199/api/";
 
    private static String token = "";
 
    public static String post(String apiPath, JSONObject paramJson, PageInfo pageInfo){
        HttpURLConnection connection=null;
        try {
            URL url = new URL(BaseHost+apiPath);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(3000);
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(false);
            connection.setRequestProperty("content-type","application/json;charset=UTF-8");
            if(!apiPath.equals("/api/getToken")){
                connection.setRequestProperty("userToken","");
            }
            /*if(pageInfo.isPage()){
                connection.setRequestProperty("pageNum",pageInfo.getPageNo()+"");
                connection.setRequestProperty("pageSize",pageInfo.getPageSize()+"");
            }*/
            connection.connect();
 
            DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
            dos.write(paramJson.toJSONString().getBytes("UTF-8"));
            //dos.writeBytes(paramJson.toJSONString());
            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new BasicException(new BasicMessage("19901","网络连接失败"));
            }
            String result = getStringByStream(connection.getInputStream());
            if (result == null) {
                throw new BasicException(new BasicMessage("19901","网络连接失败"));
            }else{
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BasicException(new BasicMessage("19901","网络连接失败"));
        }
    }
 
    public static String post(String apiPath, JSONObject paramJson){
        HttpURLConnection connection=null;
        try {
            URL url = new URL(BaseHost+apiPath);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(3000);
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("content-type","application/json;charset=UTF-8");
            if(!apiPath.equals("/api/getToken")){
                connection.setRequestProperty("userToken","");
            }
            connection.connect();
 
            DataOutputStream dos=new DataOutputStream(connection.getOutputStream());
            dos.write(paramJson.toJSONString().getBytes("UTF-8"));
            //dos.writeBytes(paramJson.toJSONString());
            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new BasicException(new BasicMessage("19901","网络连接失败"));
            }
            String result = getStringByStream(connection.getInputStream());
            if (result == null) {
                throw new BasicException(new BasicMessage("19901","网络连接失败"));
            }else{
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BasicException(new BasicMessage("19901","网络连接失败"));
        }
    }
 
    private static String getStringByStream(InputStream inputStream){
        Reader reader;
        try {
            reader=new InputStreamReader(inputStream,"UTF-8");
            char[] rawBuffer=new char[512];
            StringBuffer buffer=new StringBuffer();
            int length;
            while ((length=reader.read(rawBuffer))!=-1){
                buffer.append(rawBuffer,0,length);
            }
            return buffer.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
    public static String get(String api, Map<String,Object> map){
        HttpURLConnection connection = null;
        try {
 
            StringBuilder urlParam = new StringBuilder();
            for(String key : map.keySet()){
                urlParam.append(key+"="+map.get(key).toString()+"&");
            }
            String param = urlParam.toString();
            if(param.contains("&")){
                param = param.substring(0,param.length()-1);
            }
            URL url = new URL(BaseHost+api+"?"+param);
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(3000);
            connection.setRequestMethod("GET");
            if(!api.equals("/api/getToken")){
                connection.setRequestProperty("authorization","");
            }
 
            //dos.writeBytes(paramJson.toJSONString());
            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                return null;
            }
            String result = getStringByStream(connection.getInputStream());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
}