|
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
public static String sendPost(String url, Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
if (null==response)
return "";
HttpEntity entity1 = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity1);
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return result;
}
public static String sendPostWithAuth(String url, String authInfo) {
String username="test";
String password="test123";
CloseableHttpClient httpclient = HttpClients.createDefault();
String encoding = Base64.getEncoder().encodeToString((username+":"+ password).getBytes());
System.out.println(encoding);
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Authorization", "Basic " + encoding);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget);
int status = response.getStatusLine().getStatusCode();
System.out.println(String.valueOf(status));
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity1 = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity1);
System.out.println(result);
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return result;
} |
|