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;
	}
}

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