Http访问网络之GET和POST
public class HttpGetUtil { // 用get请求获取输入流 public static InputStream getInputStream() { InputStream inputStream = null; HttpURLConnection httpURLConnection = null; try { URL url = new URL("http://www.baidu.com/img/bd_logo1.png"); if (url != null) { httpURLConnection = (HttpURLConnection) url.openConnection(); // 设置连接网络的超时时间 httpURLConnection.setConnectTimeout(3000); // 打开输入流 httpURLConnection.setDoInput(true); // 使用GET方法请求 httpURLConnection.setRequestMethod("GET"); // 获取结果响应码 int responseCode = httpURLConnection.getResponseCode(); if (responseCode == 200) { inputStream = httpURLConnection.getInputStream(); } } } catch (Exception e) { e.printStackTrace(); } return inputStream; } // 将输入流存在本地 public void saveImageToDisk() { InputStream inputStream = getInputStream(); byte[] data = new byte[1024]; int len = 0; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("D://test.png"); while ((len = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
public class HttpPostUtil { private static URL url; // 用post请求获取输入流 public static String sendPostMessage(Map<String, String> params, String encode) { // 初始化 StringBuffer buffer = new StringBuffer(); buffer.append("?"); try { if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { // 完成转码 buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encode)) .append("&"); } // 删掉最后一个& buffer.deleteCharAt(buffer.length() - 1); } HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(3000); urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true);// 从服务器获取数据 urlConnection.setDoOutput(true);// 向服务器写数据 // 获得上传信息的字节大小以及长度 byte[] mydata = buffer.toString().getBytes(); // 表示设置请求体的类型是文本类型 urlConnection.setRequestProperty("Content-type", "application/x-www-from-urlencoded"); urlConnection.setRequestProperty("Content-length", String.valueOf(mydata.length)); // 获得输出流,向服务器输出数据 OutputStream outputStream = urlConnection.getOutputStream(); outputStream.write(mydata, 0, mydata.length); outputStream.close(); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { return changeInputStream(urlConnection.getInputStream(), encode); } } catch (Exception e) { e.printStackTrace(); } return ""; } // 将一个输入流转化成字符串 private static String changeInputStream(InputStream inputStream, String encode) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; String result = ""; if (inputStream != null) { try { while ((len = inputStream.read()) != -1) { outputStream.write(data, 0, len); } result = new String(outputStream.toByteArray(), encode); } catch (Exception e) { e.printStackTrace(); } } return result; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。