Android 网络编程之HttpURLConnection
2012-12-17 00:00
363 查看
利用HttpURLConnection对象,我们可以从网络中获取网页数据.
利用HttpURLConnection对象,我们可以从网络中获取文件数据.
利用HttpURLConnection对象,我们可以向网络发送请求参数.
利用HttpURLConnection对象,我们可以向网络发送xml数据.
<!-- lang: java --> URL url = new URL("http://www.sohu.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(6* 1000);//设置连接超时 if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败"); InputStream is = conn.getInputStream();//得到网络返回的输入流 String result = readData(is, "GBK"); conn.disconnect(); System.out.println(result); //第一个参数为输入流,第二个参数为字符集编码 public static String readData(InputStream inSream, String charsetName) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len = inSream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inSream.close(); return new String(data, charsetName); }
利用HttpURLConnection对象,我们可以从网络中获取文件数据.
<!-- lang: java --> URL url = new URL("http://photocdn.sohu.com/20100125/Img269812337.jpg"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(6* 1000); if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败"); InputStream is = conn.getInputStream(); readAsFile(is, "Img269812337.jpg"); public static void readAsFile(InputStream inSream, File file) throws Exception{ FileOutputStream outStream = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = -1; while( (len = inSream.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } outStream.close(); inSream.close(); }
利用HttpURLConnection对象,我们可以向网络发送请求参数.
<!-- lang: java --> String requestUrl = "http://localhost:8080/itcast/contanctmanage.do"; Map<String, String> requestParams = new HashMap<String, String>(); requestParams.put("age", "12"); requestParams.put("name", "中国"); StringBuilder params = new StringBuilder(); for(Map.Entry<String, String> entry : requestParams.entrySet()){ params.append(entry.getKey()); params.append("="); params.append(URLEncoder.encode(entry.getValue(), "UTF-8")); params.append("&"); } if (params.length() > 0) params.deleteCharAt(params.length() - 1); byte[] data = params.toString().getBytes(); URL realUrl = new URL(requestUrl); HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(); conn.setDoOutput(true);//发送POST请求必须设置允许输出 conn.setUseCaches(false);//不使用Cache conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive");//维持长连接 conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(data); outStream.flush(); if( conn.getResponseCode() == 200 ){ String result = readAsString(conn.getInputStream(), "UTF-8"); outStream.close(); System.out.println(result); }
利用HttpURLConnection对象,我们可以向网络发送xml数据.
<!-- lang: java --> StringBuilder xml = new StringBuilder(); xml.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); xml.append("<M1 V=10000>"); xml.append("<U I=1 D=\"N73\">中国</U>"); xml.append("</M1>"); byte[] xmlbyte = xml.toString().getBytes("UTF-8"); URL url = new URL("http://localhost:8080/itcast/contanctmanage.do?method=readxml"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(6* 1000); conn.setDoOutput(true);//允许输出 conn.setUseCaches(false);//不使用Cache conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive");//维持长连接 conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length)); conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(xmlbyte);//发送xml数据 outStream.flush(); if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败"); InputStream is = conn.getInputStream();//获取返回数据 String result = readAsString(is, "UTF-8"); outStream.close();
相关文章推荐
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- HttpURLConnection----Android网络编程
- Android 网络编程之HttpURLConnection运用
- Android 网络编程之---HttpClient 与 HttpURLConnection 共用cookie
- Android 网络编程之HttpURLConnection(conn.getResponseCode() != 200,从url读取写入数据)
- android网络编程之HttpUrlConnection的讲解--GET请求
- Android 网络编程(三)HttpURLConnection
- Android之网络编程(二)使用 HttpURLConnection
- android网络编程之HttpUrlConnection的讲解--实现文件的断点上传
- Android网络编程 HttpUrlConnection HttpClient AsyncTask
- Android 网络编程之---HttpClient 与 HttpURLConnection 共用cookie
- Android开发使用HttpURLConnection进行网络编程详解【附源码下载】
- Android网络编程---HttpURLConnection
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- Android网络编程之HttpUrlConnection、HttpClient
- android 网络编程 - HttpURLConnection与HttpClient
- android网络编程之HttpUrlConnection的讲解--上传大文件
- Android网络编程原理及HttpURLConnection 用法
- android网络编程之HttpUrlConnection--POST请求
- android网络编程之HttpUrlConnection的讲解--POST请求