| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package com.happy.Until;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * http post 提交 和 get请求
- *
- * @author QT-666
- *
- */
- public class HttpUtils {
- private static RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(15000).setConnectTimeout(15000)
- .setConnectionRequestTimeout(15000).build();
- public static String get(String url, Map<String, String> params) {
- CloseableHttpClient httpClient = null;
- HttpGet httpGet = null;
- String re = "";
- try {
- httpClient = HttpClients.createDefault();
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(20000).setConnectTimeout(20000).build();
- String ps = "";
- for (String pKey : params.keySet()) {
- if (!"".equals(ps)) {
- ps = ps + "&";
- }
- ps = pKey + "=" + params.get(pKey);
- }
- if (!"".equals(ps)) {
- url = url + "?" + ps;
- }
- httpGet = new HttpGet(url);
- httpGet.setConfig(requestConfig);
- CloseableHttpResponse response = httpClient.execute(httpGet);
- HttpEntity httpEntity = response.getEntity();
- re = EntityUtils.toString(httpEntity, "utf-8");
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (httpGet != null) {
- httpGet.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return re;
- }
- /**
- * 发送http post请求
- *
- * @param
- *
- * @param
- *
- */
- public static String post(String url, Map<String, String> params) throws IOException {
- CloseableHttpClient httpClient = null;
- HttpPost httpPost = null;
- String re = "";
- try {
- httpClient = HttpClients.createDefault();
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(20000).setConnectTimeout(20000).build();
- httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- List<NameValuePair> ps = new ArrayList<NameValuePair>();
- for (String pKey : params.keySet()) {
- ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
- }
- httpPost.setEntity(new UrlEncodedFormEntity(ps));
- CloseableHttpResponse response = httpClient.execute(httpPost);
- HttpEntity httpEntity = response.getEntity();
- re = EntityUtils.toString(httpEntity, "utf-8");
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (httpPost != null) {
- httpPost.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return re;
- }
- /**
- * 发送post请求Https,参数是字符串
- *
- * @param httpPost
- * @return
- */
- public static String post(String url, String body) throws Exception {
- String str = "";
- CloseableHttpClient httpClient = null;
- HttpPost httpPost = null;
- try {
- httpClient = HttpClients.createDefault();
- RequestConfig requestConfig = RequestConfig.custom()
- .setSocketTimeout(20000).setConnectTimeout(20000).build();
- httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- httpPost.setEntity(new StringEntity(body, "utf-8"));
- CloseableHttpResponse response = httpClient.execute(httpPost);
- HttpEntity httpEntity = response.getEntity();
- str = EntityUtils.toString(httpEntity, "utf-8");
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (httpPost != null) {
- httpPost.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return new String(str.getBytes("iso-8859-1"));
- }
- public static String get(String strURL) throws Exception {
- URL url = new URL(strURL);
- HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
- httpConn.setRequestMethod("GET");
- httpConn.connect();
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- httpConn.getInputStream(), "utf-8"));
- String line;
- StringBuffer buffer = new StringBuffer();
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
- reader.close();
- httpConn.disconnect();
- return buffer.toString();
- }
- public static void main(String[] args) throws Exception {
- SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
- String time = df.format(new Date());
- Map<String, String> req = new HashMap<>();
- req.put("operator_id", "000000002");
- req.put("operator_secret", "SBC88d0ItR3w6oCTY");
- req.put("data_secret", "Epc3hhs0imtVhPSu");
- req.put("data_secret_iv", "r57m1sr8Tg2E302L");
- req.put("sign_key", "ikAhygeGCRnEdhjgBKf");
- req.put("timeStamp", time);
- req.put("seq", "0001");
- String msg = post("http://183.129.139.222:10022/ems-share-api/queryToken", req);
- System.out.println("aaa: "+msg);
- }
- }
|