<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
package com.cncr.dcontroller.restinvoke;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class RestfulOper {
private static final Logger LOG = LoggerFactory.getLogger(RestfulOper.class);
public static String post(String url, String inputInfo) {
LOG.debug("post, url:{}, input:{}", url, inputInfo);
try {
HttpClient httpClient = HttpClients.createDefault();
url = url.replace(" ", "%20");
HttpPost httpPostRequest = new HttpPost(url);
String encoding = DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpPostRequest.setHeader("Authorization", "Basic " + encoding);
httpPostRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPostRequest.setHeader("Accept", "application/json, text/plain,");
StringEntity se = new StringEntity(inputInfo, Charset.forName("UTF8"));
httpPostRequest.setEntity(se);
HttpResponse response = httpClient.execute(httpPostRequest);
response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
try (Scanner scanner = new Scanner(entity.getContent(), "UTF-8")) {
String str = scanner.useDelimiter("\\A").next();
LOG.trace(str);
return str;
} catch (Exception e) {
LOG.error("url:{}, input:{}, error message:{}", url, inputInfo, ExceptionUtils.getStackTrace(e));
}
} catch (Exception e) {
LOG.error("url:{}, input:{}, error message:{}", url, inputInfo, ExceptionUtils.getStackTrace(e));
}
return "";
}
public static RestfulOperResult postNew(String url, String inputInfo) throws ClientProtocolException, IOException {
LOG.debug("post, url:{}, input:{}", url, inputInfo);
HttpClient httpClient = HttpClients.createDefault();
url = url.replace(" ", "%20");
HttpPost httpPostRequest = new HttpPost(url);
String encoding = DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpPostRequest.setHeader("Authorization", "Basic " + encoding);
httpPostRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPostRequest.setHeader("Accept", "application/json, text/plain,");
StringEntity se = new StringEntity(inputInfo, Charset.forName("UTF8"));
httpPostRequest.setEntity(se);
HttpResponse response = httpClient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
try (Scanner scanner = new Scanner(entity.getContent(), "UTF-8")) {
String str = scanner.useDelimiter("\\A").next();
LOG.trace(str);
return new RestfulOperResult(response.getStatusLine().getStatusCode(), str);
} catch (NoSuchElementException e) {
return new RestfulOperResult(response.getStatusLine().getStatusCode(), response.getStatusLine().toString());
}
}
public static String delete(String url) {
try {
HttpClient httpClient = HttpClients.createDefault();
url = url.replace(" ", "%20");
HttpDelete httpPostRequest = new HttpDelete(url);
String encoding = DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpPostRequest.setHeader("Authorization", "Basic " + encoding);
httpPostRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPostRequest.setHeader("Accept", "application/json, text/plain,");
HttpResponse response = httpClient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
try (Scanner scanner = new Scanner(entity.getContent(), "UTF-8")) {
String str = scanner.useDelimiter("\\A").next();
LOG.trace(str);
return str;
} catch (Exception e) {
LOG.error("url:{}, error message:{}", url, ExceptionUtils.getStackTrace(e));
}
} catch (Exception e) {
LOG.error("url:{}, error message:{}", url, ExceptionUtils.getStackTrace(e));
}
return "";
}
public static String put(String url, String inputInfo) {
try {
HttpClient httpClient = HttpClients.createDefault();
url = url.replace(" ", "%20");
HttpPut httpPostRequest = new HttpPut(url);
String encoding = DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpPostRequest.setHeader("Authorization", "Basic " + encoding);
httpPostRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPostRequest.setHeader("Accept", "application/json, text/plain,");
StringEntity se = new StringEntity(inputInfo, Charset.forName("UTF8"));
httpPostRequest.setEntity(se);
HttpResponse response = httpClient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
try (Scanner scanner = new Scanner(entity.getContent(), "UTF-8")) {
String str = scanner.useDelimiter("\\A").next();
LOG.trace(str);
return str;
} catch (Exception e) {
LOG.error("url:{}, input:{}, error message:{}", url, inputInfo, ExceptionUtils.getStackTrace(e));
}
} catch (Exception e) {
LOG.error("url:{}, input:{}, error message:{}", url, inputInfo, ExceptionUtils.getStackTrace(e));
}
return "";
}
public static String get(String url) {
try {
HttpClient httpClient = HttpClients.createDefault();
url = url.replace(" ", "%20");
HttpGet httpPostRequest = new HttpGet(url);
String encoding = DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpPostRequest.setHeader("Authorization", "Basic " + encoding);
httpPostRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPostRequest.setHeader("Accept", "application/json, text/plain,");
HttpResponse response = httpClient.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
try (Scanner scanner = new Scanner(entity.getContent(), "UTF-8")) {
String str = scanner.useDelimiter("\\A").next();
LOG.trace(str);
return str;
} catch (Exception e) {
LOG.error("url:{}, error message:{}", url, ExceptionUtils.getStackTrace(e));
}
} catch (Exception e) {
LOG.error("url:{}, error message:{}", url, ExceptionUtils.getStackTrace(e));
}
return "";
}
}
package com.cncr.dcontroller.restinvoke;
public class RestfulOperResult {
private int status;
private String returnStr;
public boolean isResult() {
return status == 200;
}
public RestfulOperResult(int status, String returnStr) {
super();
this.status = status;
this.returnStr = returnStr;
}
@Override
public String toString() {
return "RestfulOperResult [status=" + status + ", returnStr=" + returnStr + "]";
}
public int getStatus() {
return status;
}
public String getReturnStr() {
return returnStr;
}
}