package com.xunye.silenceinstall.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class HttpUtil {
private static int CONNECTION_TIMEOUT_INT = 30000;
private static int READ_TIMEOUT_INT = 30000;
public static void main(String args[]) throws UnsupportedEncodingException {
Map<String, String> params = new HashMap<String, String>();
// params.put("username", "xyw");
// params.put("password", "xyw");
// params.put("phone", "13631505031");
String rawdata = "BxIRZWdb9ZGNxkj5Hv4KiDEL60eluaECbMa7KqSNxLpVz2g+lqDj2YDMQBjZygZth7LLS0y3w2Jk2LKu9lap2a937rEY+n4qK7cwvE7euGnZ5kyLPYoa1oQZHWI1jF+FI/bebJTcBAJNkdVeDS+bGeJg/Z1FlgzS+H1Vmv8d4fyrdrJuqpsCKjmTG8QWgdr3CniYh9i3I/NrZIgfrnp2M1pKWBkfpYfzMmVauG/fesUTrRZUxXzjVyGdk+il5iWQvA/cpzpm5+yMgqMuvADNn3GQq2tn+j2ows4eo8pxAealexdE75mSyifnkkdVZGOsLkIkKfzep8EGgqCpc8S7qiNWVlT9z/nDFamHa5HACKeioY31QCKi+GKYW+4UKKL9mBZeQwMdhmEXhopaHvQ2cR1zx1ZrQteRDyEWveEf9Yg5BUjA+xctkZwGS71eCrCGd3VI/ssAWd4y1Xuh11lzDjDZhb5tunyTm5dE7XDvP5k8Ni9z6un+j5uKkOexFmh6AQ==";
Map<String, String> maps = new HashMap<>();
maps.put("rawdata", URLEncoder.encode(rawdata, "UTF-8"));
// String res=sendPost(maps,"http://192.168.2.242:8080/system/login/usershop");
// String res = sendPost(maps, "http://14.29.178.161:8090/system/pay/face/weixin");
// getVersion();
new Thread(new Runnable() {
@Override
public void run() {
System.out.print(getVersion());
}
}).start();
}
private static String getVersion(){
String res="";
// String url="https://open.clife.cn/gw/plugin/list";
String url="http://10.6.14.2:33347/gw/plugin/list";
try {
// JSONObject jsonObject=new JSONObject();
// jsonObject.put("size",10);
// jsonObject.put("current",1);
// jsonObject.put("type",2);
Map<String, String> maps = new HashMap<>();
// maps.put("pageRows","10");
// maps.put("pageIndex","1");
maps.put("type","2");
// maps.put("root", new JSONObject(jsonObject.toString()).toString());
res=HttpUtil.sendPost(maps,url);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public static String sendJsonPost(String URL,String content){
String result = ""; // 获取服务器返回数据
java.net.URL url = null;
BufferedReader bufferReader = null;
HttpURLConnection urlConn = null;
try {
url = new URL(URL);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput(true); // 设置输入流采用字节流
urlConn.setDoOutput(true);// 设置输出流采用字节流
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false); // 设置缓存
urlConn.setRequestProperty("Charset", "utf-8");
// urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.setConnectTimeout(CONNECTION_TIMEOUT_INT);
urlConn.setReadTimeout(READ_TIMEOUT_INT);
urlConn.connect(); // 连接既往服务端发送消息
DataOutputStream dop = new DataOutputStream(
urlConn.getOutputStream());
dop.write(content.getBytes("utf-8")); // 发送参数
dop.flush(); // 发送,清空缓存
dop.close(); // 关闭
int statusCode = urlConn.getResponseCode();
System.out.println("post statusCode==" + statusCode);
if (statusCode == 200) {
// 下面开始做接收工作
InputStreamReader inputStream = new InputStreamReader(
urlConn.getInputStream());
bufferReader = new BufferedReader(inputStream);
String readLine = null;
while ((readLine = bufferReader.readLine()) != null) {
result += readLine;
}
inputStream.close();
} else {
InputStreamReader inputStream = new InputStreamReader(
urlConn.getErrorStream());
bufferReader = new BufferedReader(inputStream);
String readLine = null;
while ((readLine = bufferReader.readLine()) != null) {
result += readLine;
}
inputStream.close();
}
} catch (MalformedURLException e) {
System.out.println("post MalformedURLException===" + e.toString());
e.printStackTrace();
} catch (IOException e) {
System.out.println("post IOException==" + e.toString());
e.printStackTrace();
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
if (bufferReader != null) {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String sendPost(Map<String, String> paramsMap, String URL) {
String result = ""; // 获取服务器返回数据
StringBuffer buff = new StringBuffer(); // 创建一个空StringBuffer存发给服务器的数据
// 迭代Map拼接请求参数
for (Entry<String, String> entry : paramsMap.entrySet()) {
buff.append(entry.getKey()).append('=').append(entry.getValue())
.append('&');
}
if (buff != null) {
buff.delete(buff.lastIndexOf("&"), buff.length());
java.net.URL url = null;
BufferedReader bufferReader = null;
HttpURLConnection urlConn = null;
try {
url = new URL(URL);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDoInput(true); // 设置输入流采用字节流
urlConn.setDoOutput(true);// 设置输出流采用字节流
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false); // 设置缓存
urlConn.setRequestProperty("Charset", "utf-8");
urlConn.setConnectTimeout(CONNECTION_TIMEOUT_INT);
urlConn.setReadTimeout(READ_TIMEOUT_INT);
urlConn.connect(); // 连接既往服务端发送消息
DataOutputStream dop = new DataOutputStream(
urlConn.getOutputStream());
dop.write(buff.toString().getBytes("utf-8")); // 发送参数
dop.flush(); // 发送,清空缓存
dop.close(); // 关闭
int statusCode = urlConn.getResponseCode();
System.out.println("post statusCode==" + statusCode);
if (statusCode == 200) {
// 下面开始做接收工作
InputStreamReader inputStream = new InputStreamReader(
urlConn.getInputStream());
bufferReader = new BufferedReader(inputStream);
String readLine = null;
while ((readLine = bufferReader.readLine()) != null) {
result += readLine;
}
inputStream.close();