java getcomponents_JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例

importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.utils.HttpClientUtils;importorg.apache.http.client.utils.URIBuilder;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.junit.Test;importjava.io.IOException;importjava.net.URI;importjava.net.URISyntaxException;importjava.nio.charset.Charset;importjava.util.ArrayList;importjava.util.List;public classHttpClientTest {

@Testpublic void get() throwsIOException {

CloseableHttpClient httpClient=HttpClients.createDefault();

HttpGet request= new HttpGet("https://xingzhu-song.chinacloudsites.cn/kvMapping?orgId=b11647e8-0e36-44fd-84fa-262c4fcfbe43");

request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");

CloseableHttpResponse response=httpClient.execute(request);

HttpEntity entry=response.getEntity();int code =response.getStatusLine().getStatusCode();

System.out.println("\tcode:" +code);

String result=EntityUtils.toString(entry);

System.out.println("\tresult:" +result);

HttpClientUtils.closeQuietly(response);

HttpClientUtils.closeQuietly(httpClient);

}

@Testpublic void postForm() throwsIOException {

CloseableHttpClient httpClient=HttpClients.createDefault();

HttpPost request= new HttpPost("https://xingzhu-song.chinacloudsites.cn/monitoring/metric/definitions");

List list = new ArrayList<>();

list.add(new BasicNameValuePair("orgId", "b11647e8-0e36-44fd-84fa-262c4fcfbe43"));

list.add(new BasicNameValuePair("instanceId", "1b0acfbb-d7f2-4f0d-a026-734c686ee4ba"));

request.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));

CloseableHttpResponse response=httpClient.execute(request);

HttpEntity entry=response.getEntity();int code =response.getStatusLine().getStatusCode();

System.out.println("\tcode:" +code);

String result=EntityUtils.toString(entry);

System.out.println("\tresult:" +result);

HttpClientUtils.closeQuietly(response);

HttpClientUtils.closeQuietly(httpClient);

}

@Testpublic void postJSON() throwsIOException {

CloseableHttpClient httpClient=HttpClients.createDefault();

HttpPost request= new HttpPost("https://xingzhu-song.chinacloudsites.cn/kvMapping/valid?orgId=b11647e8-0e36-44fd-84fa-262c4fcfbe43");

String body= "{" +

" \"serviceId\": \"pip\",\n" +

" \"key\": \"pipDnsLabel\",\n" +

" \"value\": \"abcdefg\",\n" +

" \"dependenceOn\": {\"pipLocation\":\"chinanorth\"}\n" +

"}";

request.setEntity(new StringEntity(body, "UTF-8"));

request.setHeader("content-type", "application/json");

CloseableHttpResponse response=httpClient.execute(request);

HttpEntity entry=response.getEntity();int code =response.getStatusLine().getStatusCode();

System.out.println("\tcode:" +code);

String result=EntityUtils.toString(entry);

System.out.println("\tresult:" +result);

HttpClientUtils.closeQuietly(response);

HttpClientUtils.closeQuietly(httpClient);

}

@Testpublic void testUrl() throwsURISyntaxException {

URIBuilder uriBuilder= new URIBuilder("https://xingzhu-song.chinacloudsites.cn");

uriBuilder.setPath("kvMapping");

uriBuilder.setCharset(Charset.forName("UTF-8"));

uriBuilder.setParameter("key1", "value1");

uriBuilder.setParameter("key2", "value2");//uriBuilder.setUserInfo("username","password");//https://username:password@xingzhu-song.chinacloudsites.cn/kvMapping?key1=value1&key2=value2

URI uri =uriBuilder.build();

System.out.println(uri);//https://xingzhu-song.chinacloudsites.cn/kvMapping?key1=value1&key2=value2

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值