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"));
}
}
待完善