zoukankan      html  css  js  c++  java
  • HttpClient的用法

    客户端模拟http请求工具

    Postmen(谷歌插件)、RestClient

    服务器模拟http请求工具

    httpclient、HttpURLConnection

    httpCient请求代码

    /**

     * 发送 post请求访问本地应用并根据传递参数不同返回不同结果

     */

    public void post() {

    // 创建默认的httpClient实例.

    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 创建httppost

    HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");

    // 创建参数队列

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    formparams.add(new BasicNameValuePair("type", "house"));

    UrlEncodedFormEntity uefEntity;

    try {

    uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

    httppost.setEntity(uefEntity);

    System.out.println("executing request " + httppost.getURI());

    CloseableHttpResponse response = httpclient.execute(httppost);

    try {

    HttpEntity entity = response.getEntity();

    if (entity != null) {

    System.out.println("--------------------------------------");

    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));

    System.out.println("--------------------------------------");

    }

    } finally {

    response.close();

    }

    } catch (ClientProtocolException e) {

    e.printStackTrace();

    } catch (UnsupportedEncodingException e1) {

    e1.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    // 关闭连接,释放资源

    try {

    httpclient.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

     

    /**

     * 发送 get请求

     */

    public void get() {

    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {

    // 创建httpget.

    HttpGet httpget = new HttpGet("http://www.baidu.com/");

    System.out.println("executing request " + httpget.getURI());

    // 执行get请求.

    CloseableHttpResponse response = httpclient.execute(httpget);

    try {

    // 获取响应实体

    HttpEntity entity = response.getEntity();

    System.out.println("--------------------------------------");

    // 打印响应状态

    System.out.println(response.getStatusLine());

    if (entity != null) {

    // 打印响应内容长度

    System.out.println("Response content length: " + entity.getContentLength());

    // 打印响应内容

    System.out.println("Response content: " + EntityUtils.toString(entity));

    }

    System.out.println("------------------------------------");

    } finally {

    response.close();

    }

    } catch (ClientProtocolException e) {

    e.printStackTrace();

    } catch (ParseException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    // 关闭连接,释放资源

    try {

    httpclient.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

    前端ajax请求

    $.ajax({

    type : 'post',

    dataType : "text",

    url : "http://a.a.com/a/FromUserServlet",

    data : "userName=小明&userAge=24",

    success : function(msg) {

    alert(msg);

    }

    });

  • 相关阅读:
    redhat 7.2 内网安装docker
    使用dockerfile 创建ubuntu ssh镜像
    docker 离线环境安装oracle
    redhat 6.6 、7、Centos7离线安装docker
    用命令行管理aws s3
    Anaconda介绍、安装及使用教程
    python2 编码问题万能钥匙
    从mongo数据库中导出数据的方法
    MongoDB学习第三篇 --- Insert操作
    MongoDB学习笔记(一)-Insert操作
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10492588.html
Copyright © 2011-2022 走看看