原地址:http://blog.csdn.net/zimo2013/article/details/9469543
关于HttpClient用法
1.HttpURLConnection实现步骤
(1).得到HttpURLConnection对象,通过调用URL.openConnection()方法得到该对象 (2).设置请求头属性,比如数据类型,数据长度等等 (3).可选的操作 setDoOutput(true),默认为false无法向外写入数据!setDoInput(true),一般不用设置默认为true (4).浏览器向服务器发送的数据,比如post提交form表单或者像服务器发送一个文件 (5).浏览器读取服务器发来的相应,包括servlet写进response的头数据(content-type及content-length等等),body数据 (6).调用HttpURLConnection的disconnect()方法, 即设置 http.keepAlive = false;释放资源
2.GZIP压缩
对于文本数据,特别是json数据或者html网页数据,最好使用gzip进行压缩,理论上文本数据可以压缩为原来的1/3,效果很明显,压缩之后应该使用gzip流进行解压缩!
[java]
view plain
copy
conn.setRequestProperty("Accept-Encoding", "gzip"); InputStream is = new BufferedInputStream(conn.getInputStream()); String encoding = conn.getContentEncoding(); if(encoding!=null && encoding.contains("gzip")){ is = new GZIPInputStream(is); }
3.简单应用
(1).get请求
[java]
view plain
copy
public static void get() { HttpURLConnection conn = null; try { URL url = new URL("http://127.0.0.1:8080/Day18/servlet/UploadTest"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setUseCaches(false); if (conn.getResponseCode() == 200) { InputStream in = new BufferedInputStream(conn.getInputStream()); String year = conn.getHeaderField("year"); System.out.println("year="+year); byte[] bytes = readFromInput(in); System.out.println(new String(bytes, "utf-8")); System.out.println("[浏览器]成功!"); } else { System.out.println("请求失败!"); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } }
(2).post表单提交
[java]
view plain
copy
public static void post() { HttpURLConnection conn = null; try { URL url = new URL("http://127.0.0.1:8080/Day18/servlet/Logining"); String para = new String("username=admin&password=admin"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("contentType","application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(para.getBytes().length)); conn.setDoOutput(true); conn.getOutputStream().write(para.getBytes()); if (conn.getResponseCode() == 200) { System.out.println("服务器已经收到表单数据!"); } else { System.out.println("请求失败!"); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } }
(3).post文件上传
[java]
view plain
copy
private static void uploadFile() { HttpURLConnection conn = null; OutputStream out = null; InputStream in = null; FileInputStream fin = null; String filePath = "c:\\android帮助文档.rar"; try { fin = new FileInputStream(filePath); conn = (HttpURLConnection) new URL("http://127.0.0.1:8080/Day18/servlet/UploadTest").openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("content-type", "application/x-rar-compressed"); conn.setFixedLengthStreamingMode(fin.available()); String fileName = filePath.substring(filePath.lastIndexOf("\\")+1); conn.setRequestProperty("filename", fileName); out = new BufferedOutputStream(conn.getOutputStream()); long totalSize = fin.available(); long currentSize = 0; int len = -1; byte[] bytes = new byte[1024*5]; while ((len = fin.read(bytes)) != -1) { out.write(bytes); currentSize += len; System.out.println("已经长传:"+(int)(currentSize*100/(float)totalSize)+"%"); } System.out.println("上传成功!"); } catch (IOException e) { e.printStackTrace(); } finally{ if(conn != null){ conn.disconnect(); } } }
NOTE:Android使用时,应该添加网络权限
[html]
view plain
copy
<uses-permission android:name="android.permission.INTERNET"/>
转载请注明原文地址: https://ju.6miu.com/read-345.html