您的位置:首页 > 其它

简单的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"));

}

}

待完善
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: