java 发送http请求

使用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;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值