简单工具类HttpUtils
生活随笔
收集整理的這篇文章主要介紹了
简单工具类HttpUtils
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單記錄? ?工具類HttpUtils
public class HttpUtils {public static JSONObject getHttpResponseJson(String url, Map<String, String> param) {CloseableHttpClient httpclient = null;CloseableHttpResponse response = null;JSONObject jsonObject = null;try {//請求發起客戶端httpclient = HttpClients.createDefault();//參數集合List postParams = new ArrayList();//遍歷參數并添加到集合if (param != null) {for (Map.Entry<String, String> entry : param.entrySet()) {postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}//通過post方式訪問HttpPost post = new HttpPost(url);HttpEntity paramEntity = new UrlEncodedFormEntity(postParams, "UTF-8");post.setEntity(paramEntity);response = httpclient.execute(post);HttpEntity valueEntity = response.getEntity();String content = EntityUtils.toString(valueEntity);jsonObject = JSONObject.parseObject(content);return jsonObject;} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (httpclient != null) {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}if (response != null) {try {response.close();} catch (IOException e) {e.printStackTrace();}}}return jsonObject;}/*** 以get方式調用第三方接口* @param url* @return*/public static String doGet(String url, String AccessToken){//創建HttpClient對象CloseableHttpClient httpClient = HttpClientBuilder.create().build();HttpGet get = new HttpGet(url);try {get.addHeader("Authorization", AccessToken);get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");HttpResponse response = httpClient.execute(get);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//返回json格式String res = EntityUtils.toString(response.getEntity());return res;}} catch (IOException e) {e.printStackTrace();}return null;}public static String doGet(String url){//創建HttpClient對象CloseableHttpClient httpClient = HttpClientBuilder.create().build();HttpGet get = new HttpGet(url);try {//get.addHeader("Authorization", AccessToken);get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");HttpResponse response = httpClient.execute(get);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//返回json格式String res = EntityUtils.toString(response.getEntity());return res;}} catch (IOException e) {e.printStackTrace();}return null;}/*** Json格式提交數據到第三方接口,并返回數據** @param param 第三方接口地址* @param param 數據參數,JSON格式,可以為null*/public static String doPost(String urlStr, String param) {HttpURLConnection connection = null;InputStream is = null;InputStreamReader rsd = null;BufferedReader br = null;OutputStream os = null;OutputStreamWriter osw = null;BufferedWriter bw = null;StringBuffer sb = new StringBuffer();try {URL url = new URL(urlStr);connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setDoInput(true);connection.setDoOutput(true);connection.setUseCaches(false); //不緩存connection.setRequestProperty("connection", "Keep-Alive"); //設置保活連接connection.setRequestProperty("charset", "UTF-8"); //提交的數據編碼connection.setRequestProperty("Content-type", "application/json"); //提交的數據格式connection.setRequestProperty("accept", "application/json"); //接收的數據格式connection.setConnectTimeout(30000); //30秒連接超時connection.setReadTimeout(30000); //30秒讀取超時connection.connect();if (param != null && !"".equals(param)) {os = connection.getOutputStream();osw = new OutputStreamWriter(os);bw = new BufferedWriter(osw);bw.write(param);bw.flush();}int status = connection.getResponseCode();if (status == 200) {is = connection.getInputStream();rsd = new InputStreamReader(is, "UTF-8");br = new BufferedReader(rsd);String s;while ((s = br.readLine()) != null) {sb.append(s);}} else {return "{\"ResuleState\":\"-1\",\"Msg\":\"連接異常\"}";}} catch (MalformedURLException e) {e.printStackTrace();} catch (ProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (br != null) br.close();if (rsd != null) rsd.close();if (is != null) is.close();if (bw != null) bw.close();if (osw != null) osw.close();if (os != null) os.close();if (connection != null) connection.disconnect();} catch (IOException e) {e.printStackTrace();}}return sb.toString();} }總結
以上是生活随笔為你收集整理的简单工具类HttpUtils的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云SDK实现短信发送
- 下一篇: 阿里云短信服务SDK使用方法