POM:apache
<!-- http client--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency>
HttpClientUtil:json
GET 設置超時時間:cookie
public static String doGet(String url) { String respContent = ""; try { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); HttpGet httpGet = new HttpGet(url); HttpResponse resp = client.execute(httpGet); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); } else { respContent = "Http響應異常"; } } catch (Exception e) { LOGGER.error("HttpUtil get, err:", e); } return respContent; }
GET:網絡
public static String get(String urlNameString) { String result = ""; CookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCookieStore(cookieStore) .build(); try { HttpGet get = new HttpGet(urlNameString);//這裏發送get請求 BasicClientCookie cookie = new BasicClientCookie("token", "49ba41f0-862b-4c31-add8-b07066b777d9"); cookie.setDomain("xxx.com.cn"); cookie.setPath("/"); cookieStore.addCookie(cookie); // 經過請求對象獲取響應對象 HttpResponse response = httpClient.execute(get); // 判斷網絡鏈接狀態碼是否正常(0--200都數正常) result = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
POST:app
public static String post(String urlNameString, Integer id, Boolean bo) { String result = ""; CookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultCookieStore(cookieStore) .build(); try { StringEntity entity = new StringEntity("{\"id\":\"" + id + "\",\"enable\":" + bo + "}", "utf-8");//解決中文亂碼問題 entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); HttpPost post = new HttpPost(urlNameString);//這裏發送get請求 post.setEntity(entity); BasicClientCookie cookie = new BasicClientCookie("token", "f9ed047d-f708-4cda-b1bc-d6abccbde576"); cookie.setDomain("xxx.com.cn"); cookie.setPath("/"); cookieStore.addCookie(cookie); // 經過請求對象獲取響應對象 HttpResponse response = httpClient.execute(post); // 判斷網絡鏈接狀態碼是否正常(0--200都數正常) result = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
Post Josnpost
1 public static String httpPostWithJSON(String url) throws Exception { 2 3 HttpPost httpPost = new HttpPost(url); 4 CloseableHttpClient client = HttpClients.createDefault(); 5 String respContent = null; 6 7 // json方式 8 JSONObject jsonParam = new JSONObject(); 9 jsonParam.put("name", "admin"); 10 jsonParam.put("pass", "123456"); 11 StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解決中文亂碼問題 12 entity.setContentEncoding("UTF-8"); 13 entity.setContentType("application/json"); 14 httpPost.setEntity(entity); 15 System.out.println(); 16 17 18 // 表單方式 19 // List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>(); 20 // pairList.add(new BasicNameValuePair("name", "admin")); 21 // pairList.add(new BasicNameValuePair("pass", "123456")); 22 // httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8")); 23 24 25 HttpResponse resp = client.execute(httpPost); 26 if(resp.getStatusLine().getStatusCode() == 200) { 27 HttpEntity he = resp.getEntity(); 28 respContent = EntityUtils.toString(he,"UTF-8"); 29 } 30 return respContent; 31 } 32
調用:ui
public static void main(String[] args) throws IOException { //result = HttpClientUtil.get(getUrl); String data = "643,1839,2992,3890,3991,4002,4080,4270,4277,4333,4334,4401,4495,4501,4563,4573,4584,4653,4709,4717,4732,4766,4913,5424,5699,5705,6224,6235,6329,6342,6528,6529,6552,6555,6660,6667,6923,6970,8020,8146,8982,9354,9358,9364,9522,9554,9561,9960,10059,10188"; doForeach(Arrays.asList(data.split(","))); } private static void doForeach(List<String> ids) { for (String id : ids) { String result = HttpClientUtil.post(postUrl, Integer.parseInt(id), true); System.out.println(result); } }