• ahttpClient


    public static void main(String[] args) throws IOException {
            //1.打开浏览器
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //2.声明get请求
            HttpGet httpGet = new HttpGet("http://www.baidu.com/");
            //3.发送请求
            CloseableHttpResponse response = httpClient.execute(httpGet);
            //4.判断状态码
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                //通过EntityUtils工具类转成字符串
                String string = EntityUtils.toString(entity, "utf-8");
                System.out.println(string);
            }
            //5.关闭资源
            response.close();
            httpClient.close();
        }


    public String send(String postUrl,String json,String sign,String token) {
    String params = JSON.toJSONString(json);
    System.out.println(params);
    String url = postUrl;
    HttpClient httpClient = null;
    HttpPost postMethod = null;
    HttpResponse response = null;
    try {
    httpClient = HttpClients.createDefault();
    postMethod = new HttpPost(url);//传入URL地址
    //设置请求头
    postMethod.addHeader("Content-type", "application/json");
    postMethod.addHeader("sign", sign);//设置请求头
    postMethod.addHeader("token",token);
    //传入请求参数
    postMethod.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
    response = httpClient.execute(postMethod);//获取响应
    int statusCode = response.getStatusLine().getStatusCode();
    System.out.println("HTTP Status Code:" + statusCode);
    if (statusCode != HttpStatus.SC_OK) {
    System.out.println("HTTP请求未成功!HTTP Status Code:" + response.getStatusLine());
    }
    HttpEntity httpEntity = response.getEntity();
    String reponseContent = EntityUtils.toString(httpEntity);
    EntityUtils.consume(httpEntity);//释放资源
    System.out.println("响应内容:" + reponseContent);
    return reponseContent;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return "";
    }
     
  • 相关阅读:
    京东POP2020新店新规
    hdu 1203
    hdu 1513
    有关Canvas的一点小事—canvas数据和像素点
    有关Canvas的一点小事—图像绘制
    有关Canvas的一点小事--鼠标绘图
    Javascript和jquery事件--键盘事件KeyboardEvent
    [转]DOM0,DOM2,DOM3事件处理方式区别
    Javascript和jquery事件--鼠标事件的小结
    Javascript和jquery事件--鼠标移动事件mousemove
  • 原文地址:https://www.cnblogs.com/otways/p/11561851.html
Copyright © 2020-2023  润新知