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
- Http协议(get请求和post请求)
- http协议之get 和 post请求
- HTTP协议GET和POST请求对比
- 【JAVAWEB】--HTTP协议GET和POST请求
- http协议和get请求和post请求
- HTTP协议的GET请求和POST请求
- http协议中,get请求和post请求的区别
- HTTP get和post请求
- http post和get请求
- HTTP post请求和get请求
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- HTTP 协议请求方法get和post的区别
- iOS开发-http协议、get和post请求
- 基于HTTP 协议的GET和POST请求服务
- HTTP协议请求方式:GET 和 POST区别
- 程序员面试宝典--http协议get和post请求
- 宽字节字符集和多字符集
- 人生的成功基于什么?
- LeetCode: Spiral Matrix II [058]
- iOS文字排版(CoreText)那些事儿
- Android强制弹出,隐藏输入法.
- Http协议(get请求和post请求)
- Charm Bracelet
- 登陆拦截
- 代理模式
- C连接MySQL数据库开发之Windows环境配置及测试
- Eclipse设置行宽Maximum line width
- JSON----创建JSON
- 从select的一个死循环谈epoll的ET模式
- jeesite web.xml