public static String doPost(String url, HashMap data){ String resultStr = ""; if(StringUtils.isEmpty(url)){ return resultStr; } CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; try{ httpClient = SSLClientCustom.getHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (data.size()!=0){ Set keySet = data.keySet(); Iterator it = keySet.iterator(); while (it.hasNext()){ String k = (String) it.next();// key String v = (String) data.get(k);// value nameValuePairs.add(new BasicNameValuePair(k, v)); } } httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); System.out.println("nameValuePairs:" + nameValuePairs); httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); System.out.println("entity:" + entity); if (entity != null) { // 响应的结果 String content = EntityUtils.toString(entity, "UTF-8"); System.out.println("content" + content); return content; } }catch (Exception e){ e.printStackTrace(); }finally { try{ httpResponse.close(); httpClient.close(); }catch (Exception e){ e.printStackTrace(); } } return resultStr; }
Java发送表单形式的代码
最新推荐文章于 2023-08-11 23:58:24 发布