简单的get与post请求工具类(复制即可)
2015-10-24 15:09
309 查看
package net.meixintong.vo;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class OneTest {
/**
* 提交一个post请求,并获得请求的返回值
* @return
* @throws Exception
*/
public static Object doGets(String url, String type ) throws Exception {
return esayGet(url, type);
}
/**
* 提交一个post请求,并获得请求的返回值
* @return
* @throws Exception
*/
public static Object doPosts(URL postUrl, String type) throws Exception {
return doPost(postUrl, type);
}
/**
* 简单的Get请求
* @param weburl
* @param charsetName 编码
* @return
* @throws Exception
*/
public static Object esayGet(String weburl, String charsetName) throws Exception{
HttpURLConnection connection = null;
ObjectInputStream ois = null;
OutputStreamWriter out = null;
ObjectOutputStream oos = null;
try {
URL url = new URL(weburl);
// 新建连接实例
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);// 打开写入属性
connection.setDoInput(true);// 打开读取属性
connection.setRequestMethod("GET");// 设置提交方法
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(1000 * 5);// 连接超时时间
connection.setReadTimeout(1000 * 5);// 连接超时时间
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream instream = connection.getInputStream();
byte[] data = read(instream);
String jsonStr = new String(data, charsetName);
return jsonStr;
} else{
throw new RuntimeException(weburl + "无法获取数据,返回类型为:" + connection.getResponseCode());
}
} finally {
close(out);
close(oos);
close(ois);
if (connection != null) {
connection.disconnect();
}
}
}
/**
* 返回json数据专用
* @param postUrl 请求的url需加 ?
* @param cont
* @return json
*/
public static String doPost(URL postUrl,String cont){
String line = null;
try {
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(cont);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
while ((line = reader.readLine()) != null) {
return line;
}
reader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
public static void close(Closeable out) {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
}
}
/**
* 读取输入流为byte[]数组
*/
public static byte[] read(InputStream instream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
public static void main(String[] args) throws Exception {
System.out.println( esayGet("http://datachina.win/app/getTest.json", "utf-8"));
}
}
待完善
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class OneTest {
/**
* 提交一个post请求,并获得请求的返回值
* @return
* @throws Exception
*/
public static Object doGets(String url, String type ) throws Exception {
return esayGet(url, type);
}
/**
* 提交一个post请求,并获得请求的返回值
* @return
* @throws Exception
*/
public static Object doPosts(URL postUrl, String type) throws Exception {
return doPost(postUrl, type);
}
/**
* 简单的Get请求
* @param weburl
* @param charsetName 编码
* @return
* @throws Exception
*/
public static Object esayGet(String weburl, String charsetName) throws Exception{
HttpURLConnection connection = null;
ObjectInputStream ois = null;
OutputStreamWriter out = null;
ObjectOutputStream oos = null;
try {
URL url = new URL(weburl);
// 新建连接实例
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);// 打开写入属性
connection.setDoInput(true);// 打开读取属性
connection.setRequestMethod("GET");// 设置提交方法
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(1000 * 5);// 连接超时时间
connection.setReadTimeout(1000 * 5);// 连接超时时间
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream instream = connection.getInputStream();
byte[] data = read(instream);
String jsonStr = new String(data, charsetName);
return jsonStr;
} else{
throw new RuntimeException(weburl + "无法获取数据,返回类型为:" + connection.getResponseCode());
}
} finally {
close(out);
close(oos);
close(ois);
if (connection != null) {
connection.disconnect();
}
}
}
/**
* 返回json数据专用
* @param postUrl 请求的url需加 ?
* @param cont
* @return json
*/
public static String doPost(URL postUrl,String cont){
String line = null;
try {
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(cont);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
while ((line = reader.readLine()) != null) {
return line;
}
reader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
public static void close(Closeable out) {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
}
}
/**
* 读取输入流为byte[]数组
*/
public static byte[] read(InputStream instream) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
public static void main(String[] args) throws Exception {
System.out.println( esayGet("http://datachina.win/app/getTest.json", "utf-8"));
}
}
待完善
相关文章推荐
- HTTP POST GET 本质区别详解
- CVX基础
- EasyUI - ValidateBox 验证组件
- 数据挖掘中,Apriori算法讲解
- 一看就会Android之SQLite数据库的使用思路
- delphi 指针 认识
- 45
- IDEA Information:java: javacTask: 源发行版 1.7 需要目标发行版 1.7
- [No00002B]知乎精选:如果兔子都在拼命奔跑,乌龟该如何前进
- 微信公众平台开发模式 微信 语音识别 公众平台 消息接口 语音识别
- String类的使用说明
- iOS开发使用MJRefresh进行刷新
- 基于React.js实现webapp的技术实践
- 基于Reactjs实现webapp(加精)
- 基于React.js实现webapp的技术实践
- JSON数据结构学习
- 案例研究:CopyToAsync
- 访问局域网内数据库
- 第三章 使用Servlet处理HTTP响应
- [Leetcode]Palindrome Partitioning