使用jersey
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import net.sf.json.JSONObject;
import com.chinaUnicom.entitys.OrgObject;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class CommonClient {
public static String restGet(String restUrl) throws Exception{
Client client = ClientBuilder.newBuilder().build();
Response response = client.target(restUrl).request().get();
String result = null;
if(Response.Status.OK.getStatusCode() ==response.getStatusInfo().getStatusCode()){
result = response.readEntity(String.class);
}else{
System.out.println(response.getStatusInfo().getStatusCode());
throw new Exception(String.format("%s call failed", restUrl));
}
return result;
}
public static String restPost(String restUrl,MultivaluedMap<String, String> formData) throws Exception{
Client client = ClientBuilder.newBuilder().build();
Response response = client.target(restUrl).request().post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED));
String result = null;
if(Response.Status.OK.getStatusCode() ==response.getStatusInfo().getStatusCode()){
result = response.readEntity(String.class);
}else{
System.out.println(response.getStatusInfo().getStatusCode());
throw new Exception(String.format("%s call failed", restUrl));
}
return result;
}
public static void main(String[] args) throws Exception {
// MultivaluedMap par = new MultivaluedMapImpl();
// par.add("lifeOrder", "CREATE_CTNR");
// par.add("operationId", "121312");
// par.add("containerConfigId", "1145");
// par.add("statusType", "0");
// String s = CommonClient.restPost("http://10.0.210.57:8080/ctnrDefCtrl/rest/containerLifeManage/sendContainerLifeOrder",par);
//
//String s = CommonClient.restGet("http://10.0.209.182:8000/Product/GetProducts");
// String s = CommonClient.restGet("http://10.0.209.182:9000/App/GetApps/1");
// System.out.println(s);
//
// System.out.println(s);
//Sys/AddOrgInf
String url = "http://10.0.209.182:9000/Sys/AddOrgInfo";
JSONObject json = JSONObject.fromObject(info);
MultivaluedMap par = new MultivaluedMapImpl();
par.add("orgInfo", json.toString());
// par.add("description", "description");
// par.add("fax", "fax");
// par.add("orgCode", "orgCode");
System.out.println(String.format("请求路径及参数为%s", url));
String s = CommonClient.restPost(url,par);
}
}
下面是发送json对象的例子
public static String jsonPost( String url, String json,String publicKey) throws Exception{
baseLog.info("url:" + url);
baseLog.info("jsonData:" + json);
String result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
httpPost.addHeader("Access-Token", publicKey);
StringEntity se = new StringEntity(json,"utf-8");
se.setContentType(CONTENT_TYPE_TEXT_JSON);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
se.getContentLength();
httpPost.setEntity(se);
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClientUtil.getInstance().createClient();
response = client.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, CHARSET);
EntityUtils.consume(entity);
}
if (statusCode == 200) {
// baseLog.info("growingio info: success");
} else {
// baseLog.warn("growingio info:error detail:" + result);
}
httpPost.abort();
} catch (Exception e) {
e.printStackTrace();
// baseLog.warn("growingio info:error detail:" + e.getMessage());
} finally {
if(client != null){
client.close();
}
if(response != null){
response.close();
}
}
return result;
}