• 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);

    }

    });

  • 相关阅读:
    一点创业想法
    【转】Java程序员常用工具类库
    向着高薪前进
    web开发可不可以是这样的?
    java 读取文本文件超简单的方法
    java操作xml超简单的方法
    Dijkstra算法
    ubuntu linux下如何配置ip地址以及DNS
    有关于string的一些用法
    Linux mint 17.2 系统下安装hust oj
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10492588.html
Copyright © 2020-2023  润新知