zoukankan      html  css  js  c++  java
  • 简易的RestClient代码

    package tests;
     
    import java.io.*;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
     
    /**
     * This class is the same as the ApacheHttpRestClient2 class, but with
     * fewer try/catch clauses, and fewer comments.
    */
    public class ApacheHttpRestClient3 {
     
      public final static void main(String[] args) {
         
        HttpClient httpClient = new DefaultHttpClient();
        try {
          HttpGet httpGetRequest = new HttpGet("http://search.twitter.com/search.json?q=%40apple");
          HttpResponse httpResponse = httpClient.execute(httpGetRequest);
     
          System.out.println("----------------------------------------");
          System.out.println(httpResponse.getStatusLine());
          System.out.println("----------------------------------------");
     
          HttpEntity entity = httpResponse.getEntity();
     
          byte[] buffer = new byte[1024];
          if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
              int bytesRead = 0;
              BufferedInputStream bis = new BufferedInputStream(inputStream);
              while ((bytesRead = bis.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                System.out.println(chunk);
              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try { inputStream.close(); } catch (Exception ignore) {}
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          httpClient.getConnectionManager().shutdown();
        }
      }
    }
    
  • 相关阅读:
    Opencv3 ——读取图像,显示图像
    QT5生成log日志
    QT5串口读取宇电温控器温度
    QSettings 配置信息写入本地文件
    Qt5应用程序封包
    python 创建虚拟环境
    Ubuntu更换国内源
    一个关于百度编辑器的小问题
    JavaScript等比例缩放图片(转载)
    asp.net io操作,修改文件夹的名称,报错:access to the path is denied
  • 原文地址:https://www.cnblogs.com/laoniu85/p/5107517.html
Copyright © 2011-2022 走看看