Http协议(get请求和post请求)

来源:互联网 发布:js for of 修改数组 编辑:程序博客网 时间:2024/05/05 22:53
import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;public class HttpUtils {/** * 下载文件到内存卡 *  * @param urlPath *            文件下载路径 * @param savePath *            文件存储路径 * @return 0代表下载失败 1代表下载成功 */public static int downloadFile(String urlPath, String savePath) {File f = new File(savePath);if(!(f.getParentFile()).exists()) {f.getParentFile().mkdirs();}int result = 1;InputStream inputStream = null;FileOutputStream fos = null;try {URL url = new URL(urlPath);URLConnection connection = url.openConnection();// 取得inputStreaminputStream = connection.getInputStream();// 创建FileOutputStream对象fos = new FileOutputStream(savePath);byte buffer[] = new byte[1024];while (inputStream.read(buffer) != -1) {fos.write(buffer);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();result = 0;} finally {try {if (fos != null) {fos.close();fos = null;}if (inputStream != null) {inputStream.close();inputStream = null;}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return result;}/** * 发送get请求 *  * @param path *            路径 * @param encode *            编码方式 * @return 返回的html内容 */public static String sendGet(String path, String encode) {String result = "";BufferedReader br = null;try {URL url = new URL(path);URLConnection connection = url.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 建立实际的连接connection.connect();// 读取内容br = new BufferedReader(new InputStreamReader(connection.getInputStream(), encode));String line = "";while ((line = br.readLine()) != null) {result += line + "\r\n";}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null) {br.close();br = null;}} catch (Exception e2) {e2.printStackTrace();}}return result;}/** * 发送post请求 *  * @param path *            路径 * @param param *            请求参数,请求参数应该是 key1=value1&key2=value2 的形式。 * @param encode *            编码方式 * @return 返回的html内容 */public static String sendPost(String path, String param, String encode) {String result = "";PrintWriter pw = null;BufferedReader br = null;try {URL url = new URL(path);URLConnection connection = url.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 发送POST请求必须设置如下两行connection.setDoOutput(true);connection.setDoInput(true);// 获取URLConnection对象对应的输出流pw = new PrintWriter(connection.getOutputStream());// 发送请求参数pw.print(param);// flush输出流的缓冲pw.flush();// 读取内容br = new BufferedReader(new InputStreamReader(connection.getInputStream(), encode));String line = "";while ((line = br.readLine()) != null) {result += line + "\r\n";}} catch (Exception e) {e.printStackTrace();} finally {try {if (br != null) {br.close();br = null;}if (pw != null) {pw.close();pw = null;}} catch (Exception e2) {e2.printStackTrace();}}return result;}}

0 0