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<String> responseHandler = new ResponseHandler<String>() {

@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();
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容