public static String postForm(String url, Map<String, String> params) { try { HttpPost httpPost = new HttpPost(url.trim()); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); BasicClientCookie cookie = new BasicClientCookie("PHPSESSID", "2brls80t268lhm9vhmadvkbko4"); httpPost.setHeader("Cookies", cookie.toString()); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build(); httpPost.setConfig(requestConfig); //StringEntity se = new StringEntity(param.toString(), CHARSET_UTF_8); List<NameValuePair> form = new ArrayList<>(); for (String name : params.keySet()) { form.add(new BasicNameValuePair(name, params.get(name))); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, CHARSET_UTF_8); httpPost.setEntity(entity); HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpPost); return EntityUtils.toString(httpResponse.getEntity(), CHARSET_UTF_8); } catch (Exception e) { e.printStackTrace(); } return ""; }
/** * Add the cookie to the request. */ public void addCookie(String name, String content, String domain, String path) { BasicClientCookie cookie = new BasicClientCookie( (name == null) ? "" : name , (content == null) ? "" : content ); cookie.setVersion(0); cookie.setDomain((domain == null) ? "" : domain); cookie.setPath((path == null) ? "" : path); log.info("Add cookie = [" + cookie.toString() + "]"); getCookieStore().addCookie(cookie); }
LOG.debug("Forwarding cookie {} -> {}", cookie.toString(), cookieToForward.toString());