HttpClient远程调用接口


    org.apache.httpcomponents
    httpclient
    4.5.3

 

package com.licheng.study.httpclient;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
/**
 * @ClassName: HttpClientTest  
 * @Description:
 * @author freeflying
 * @date 2018年8月4日
 */
public class HttpClientTest {
	public static void main(String[] args) throws Exception {
		HttpClient httpClient = HttpClientBuilder.create().build();
		//请求路径
		String url = "";
		HttpPost post=new HttpPost(url);
		post.setHeader("session","{}");
		//设置请求和传输超时时间
		Builder builder = RequestConfig.custom();
		RequestConfig requestConfig=null;
		requestConfig=builder.setSocketTimeout(2000).build();
		requestConfig=builder.setConnectTimeout(2000).build();
		post.setConfig(requestConfig);
		HttpResponse response=httpClient.execute(post);
		String string=EntityUtils.toString(response.getEntity());
		System.out.println(string);
	}
}

 

你可能感兴趣的:(javase)