httpclient

发送Post登录请求:

发送get请求:

创建连接对象:
CloseableHttpClient httpclient = HttpClients.createDefault();

设置 IP 端口 url路径 :
String host = "172.19.0.245";
String portAndPath = "8088/fastgate/personGroup/6/1";

使用try{} finally{close} 来运行以下步骤:
新建Get连接对象
HttpGet httpget = new HttpGet("http://"+host+":"+portAndPath);

打印进行连接请求:
System.out.println("Executing request "+httpget.getRequestLine());

新建响应处理方法: ResponseHandler 重写handlerResponse 方法
ResponseHandler responseHandler = new ResponseHandler() {

@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub

    //获取响应的状态
    int status = response.getStatusLine().getStatusCode();
    
    //如果响应正常
    if (status >= 200 && status <=300) {
    
        // 获取响应实体
        HttpEntity entity = response.getEntity();
        
        // 响应内容 非null判断
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {
        throw new ClientProtocolException("Unexpected response status:"+status);
    }
                
}

} ;

最后获取返回体:
String responseBody = httpclient.execute(httpget,responseHandler);
System.out.println("------------------------------");
System.out.println(responseBody);

finally {
httpclient.close();
}

你可能感兴趣的:(httpclient)