package com.gxt.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
/**
*
*@ 羅文浩
* GetSingHttpClient 獲取HttpClient 2015-1-10 下午8:41:37
*
*
@version 1.0.0
*
*/
public class SingHttpConnection extends HttpURLConnectionFactory {
Logger log = Logger.getLogger("SingHttpConnection");
public SingHttpConnection() {
}
public static SingHttpConnection getSingHttpConnection() {
return SingletonHolderHttpConnection.INSTANCE;
}
private static class SingletonHolderHttpConnection {
private static SingHttpConnection INSTANCE = new SingHttpConnection();
public static String getHttpConnection(String url,int timeount) {
HttpURLConnection connection = null;
String line = null;
String result = null;
BufferedReader reader = null;
long startTime=System.currentTimeMillis();
try {
URL postUrl = new URL(url);
connection = (HttpURLConnection) postUrl.openConnection();
connection.setConnectTimeout(timeount);
//connection.setReadTimeout(timeount);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
result = line;
}
long endTime=System.currentTimeMillis();
System.out.println("系統連接時間="+(endTime-startTime));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.disconnect();
}
return result;
}
public static String getHttpConnectionJson(String requestUrl,int timeount,String datajson) {
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
URL realUrl = new URL(requestUrl);
// 打開和URL之間的鏈接
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
// 設置通用的請求屬性
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(datajson.length()));
// 發送POST請求必須設置以下兩行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
OutputStream outStream = httpURLConnection.getOutputStream();
// 發送請求參數
byte[] data = datajson.getBytes("utf-8");
outStream.write(data);
// flush輸出流的緩衝
outStream.flush();
// 根據ResponseCode判斷鏈接是否成功
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != 200) {
System.out.println("post請求失敗");
} else {
System.out.println("post請求成功");
}
// 定義BufferedReader輸入流來讀取URL的ResponseData
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} catch (Exception e) {
System.out.println("send post request error!" + e);
} finally {
httpURLConnection.disconnect();
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return responseResult.toString();
}
}
/* (non Javadoc)
*
@Title : getDefaultHttpConnection
* @Description: TODO
*
@param url
*
@param timeount * @return * @see com.gxt.utils.HttpURLConnectionFactory#getDefaultHttpConnection(java.lang.String, int) */ @Override public String getDefaultHttpConnection(String url, int timeount) { return SingletonHolderHttpConnection.getHttpConnection(url,timeount); } /* (non Javadoc) * @Title: getDefaultHttpConnectionJson * @Description: TODO * @param url * @param timeout * @param jsonstr * @return * @see com.gxt.utils.HttpURLConnectionFactory#getDefaultHttpConnectionJson(java.lang.String, int, java.lang.String) */ @Override public String getDefaultHttpConnectionJson(String url, int timeout, String jsonstr) { // TODO Auto-generated method stub return SingletonHolderHttpConnection.getHttpConnectionJson(url, timeout, jsonstr); } }