zoukankan      html  css  js  c++  java
  • httpclient调用方法

    /**
     * GET请求
     * 
     * @param url
     *            请求url,参数拼在请求串中
     */
    public static String get(String url) {
    String responseContent = null;
    // 创建默认的httpClient实例.
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    HttpGet httpGet = new HttpGet(url);
    Logger.getRootLogger().info("--------------------------------------------");
    Logger.getRootLogger().info("GET " + httpGet.getURI());
    // 执行get请求.
    try {
    response = httpclient.execute(httpGet);
    HttpEntity entity = response.getEntity();
    // 打印响应状态
    Logger.getRootLogger().info(response.getStatusLine());
    if (entity != null) {
    responseContent = EntityUtils.toString(entity);
    // 打印响应内容
    Logger.getRootLogger().info("Response content: "+ responseContent);
    Logger.getRootLogger().info("--------------------------------------------");
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    httpclient.close();
    response.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return responseContent;
     
    }
     
    /**
     * 
     * @param url
     * @param paramMap
     */
    public static String post(String url, Map<String, String> paramMap) {
    String responseContent = null;
    // 创建默认的httpClient实例.
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建httppost
    HttpPost httppost = new HttpPost(url);
    Logger.getRootLogger().info("--------------------------------------------");
    Logger.getRootLogger().info("POST " + httppost.getURI());
    // 创建参数队列
    List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
    if (paramMap != null) {
    Set<String> key = paramMap.keySet();
    for (Iterator<String> it = key.iterator(); it.hasNext();) {
    String paramKey = (String) it.next();
    formparams.add(new BasicNameValuePair(paramKey, paramMap
    .get(paramKey)));
    Logger.getRootLogger().info(paramKey+" = "+paramMap.get(paramKey));
    }
    }
     
    UrlEncodedFormEntity uefEntity;
    try {
    uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
    httppost.setEntity(uefEntity);
    CloseableHttpResponse response = httpclient.execute(httppost);
    try {
    HttpEntity entity = response.getEntity();
    Logger.getRootLogger().info(response.getStatusLine());
    if (entity != null) {
    responseContent = EntityUtils.toString(entity, "UTF-8");
    Logger.getRootLogger().info("Response content: "+ responseContent);
    Logger.getRootLogger().info("--------------------------------------------");
    }
    } 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();
    }
    }
    return responseContent;
    }
  • 相关阅读:
    Hausdorff distance between mesh and its symmertic one.
    Fast algorithm to compute minimum volume oriented bounding box
    C++文件读写详解(ofstream,ifstream,fstream)
    libCURL开源库在VS2010环境下编译安装,配置详解
    VPB和OSGGIS安装
    OpenSceneGraph 笔记--如何导出三角形数据
    OpenGL编程指南(第七版)
    osgAnimation例子的注释的注释
    osg 示例程序解析之osgdelaunay
    VS2010+64+OSG3.2.1之五Plugins dae编译
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/4882484.html
Copyright © 2011-2022 走看看