在java中http的get和post请求
来源:互联网 发布:中国的网络环境 编辑:程序博客网 时间:2024/05/29 11:46
一个比较简单的工具类,不多解释了,直接上代码
package com.sharebo.util;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL; /** * http转发工具类 * @author 郭鹏飞 * */public class HttpUtil {/** * http get请求 * <blockquote><pre> * 返回数据拼装为String类型<p> * 如果需要返回其他类型的数据,请另写方法改动 * </pre></blockquote> * @param httpUrl * @return */public static String request_get(String httpUrl) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); connection.setDoOutput(true); connection.setDoInput(true); connection.setConnectTimeout(5000); connection.setReadTimeout(20000); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { //e.printStackTrace(); } return result;}/** * http post请求 * @param httpUrl * @param httpArg * @return */public static String request_post(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); connection.setDoOutput(true); connection.setDoInput(true); connection.setConnectTimeout(5000); connection.setReadTimeout(20000); connection.getOutputStream().write(httpArg.getBytes("UTF-8")); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result;}}
阅读全文
0 0
- 在java中http的get和post请求
- Java http的get和post请求
- Java http的get和post请求
- HTTP中GET请求和POST请求的区别
- http协议中,get请求和post请求的区别
- java中http的post和get请求,请求结果json转为对象或Map型
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- Java中发送Http请求Get、Post
- Java中发送http的get、post请求
- HTTP中GET和POST请求区别
- http中get和post请求差别
- http中get和post请求
- Android中HTTP Post和Get请求
- Http中get和post请求
- java http 请求get 和post
- java发送http get 和 post 请求
- ettercap进行简单的arp欺骗和中间人攻击
- 网页临摹12.08
- Hbase入门简介
- MyBatis传参问题
- Webdriver+Testng实现测试用例失败自动截图功能
- 在java中http的get和post请求
- Asp.net 实现只能允许一个账号同时只能在一个地方登录
- 简单并查集
- Python checkio Long Repeat解决方案
- RecyclerView 实现循环滚动
- 一文读懂Apache Kylin
- python编程思维导图
- 快速排序
- [知了堂学习笔记] javascript DOM练习案例