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

    }

    });

  • 相关阅读:
    MS CRM 2011 RC中的新特性(4)——活动方面之批量编辑、自定义活动
    最近的一些有关MS CRM 2011的更新
    MS CRM 2011 RC中的新特性(6)——连接
    MS CRM 2011 RC中的新特性(7)—仪表板
    参加MS CRM2011深度培训课程——第一天
    MS CRM 2011插件调试工具
    MS CRM2011实体介绍(四)——目标管理方面的实体
    MS CRM 2011 RC中的新特性(3)——客户服务管理方面
    MS CRM 2011 RC中的新特性(8)—数据管理
    ExtAspNet 登陆
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/10492588.html
Copyright © 2011-2022 走看看