HttpClient 和HttpURLConnection 对比

xiaoxiao2021-02-27  215

package com.example.administrator.downloadimage; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.ParseException; import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * Created by Administrator on 2017/5/4. */ public class HttpUtils { // 判断网络是否连接 public static boolean isNetWorkConn(Context context) { // 需要权限:访问网络状态 ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { return info.isConnected(); } else { return false; } } // 下载获取数据 public static byte[] httpClientLoadData(String path) { byte[] result = null; try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(path); HttpResponse response = httpClient.execute(httpget); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); result = EntityUtils.toByteArray(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } // 下载获取数据 public static byte[] httpURLConnLoadData(String path) { // 原生流操作 BufferedInputStream bis = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { HttpURLConnection huc = (HttpURLConnection) new URL(path).openConnection(); huc.setRequestMethod("GET"); huc.connect(); if (huc.getResponseCode() == 200) { bis = new BufferedInputStream(huc.getInputStream()); int i = -1; byte[] bb = new byte[100]; while ((i = bis.read(bb)) != -1) { baos.write(bb, 0, i); baos.flush(); } bis.close(); return baos.toByteArray(); } } catch (IOException e) { e.printStackTrace(); } return null; } // 下载网络数据到本地 public static void httpURLConnectionLoadData(String path) { HttpURLConnection con = null; FileOutputStream fos = null; BufferedInputStream bis = null; try { con = (HttpURLConnection) new URL(path).openConnection(); con.setRequestMethod("GET"); con.setUseCaches(false);// 忽略缓存 // 发送POST请求必须设置如下两行 //con.setDoOutput(true); // 开启输出流 //con.setDoInput(true); //设置超时 con.setConnectTimeout(5000);//连接超时 con.setReadTimeout(5000);//读取数据超时 // 设置通用的请求属性 con.setRequestProperty("Content-type", "application/json"); con.setRequestProperty("Connection", "Keep-Alive"); con.connect(); if (con.getResponseCode() == 200) { bis = new BufferedInputStream(con.getInputStream()); //写入本地工程文件中 fos = new FileOutputStream(new File("my-file.txt")); int len = -1; byte[] bytes = new byte[1024]; while ((len = bis.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.flush(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null) { bis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } // 关闭连接 con.disconnect(); } }
转载请注明原文地址: https://www.6miu.com/read-12743.html

最新回复(0)