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

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();
			// 取得inputStream
			inputStream = 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 block
			e.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 block
				e.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;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章