private static final int REQUEST_TIMEOUT = 20 * 1000;// 设置请求超时10秒钟
public static String encode(String URL)
{
String result = URL;
if (CommonUtil.isNotEmpty(URL))
{
try
{
result = URLEncoder.encode(URL, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
return result;
}
public static String getInfomations(String url)
{
String value = null;
try
{
if (CommonUtil.isNotEmpty(url))
{
url= url.replaceAll(" ", "%20");
/*
* 创建一个http客户端
*/
HttpClient client = new DefaultHttpClient();
/*
* 设置超时处理
*/
getHttpClient(client);
/*
* 创建一个GET请求
*/
HttpGet httpGet = new HttpGet(url);
/*
* 向服务器发送请求并获取服务器返回的结果
*/
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity)
{
/*
* 返回的结果可能放到InputStream,http
*/
InputStream inputStream = entity.getContent();
/*
* 解析流
*/
value = Parser.Reader(inputStream);
}
else
{
CommonUtil.log("server entity is null...");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return value;
}
public static void getHttpClient(HttpClient client) {
HttpParams httpParams = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
// HttpClient client = new DefaultHttpClient(httpParams);
}
/**
*
* parser inputStream
*
* @param inputStream
* @return
*/
public static String Reader(InputStream inputStream)
{
String sCurrentLine = null;
String sTotalString = null;
try
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stemp = new StringBuffer();
while ((sCurrentLine = bufferedReader.readLine()) != null)
{
stemp.append(sCurrentLine);
}
sTotalString = stemp.toString();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != inputStream)
{
try
{
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sTotalString;
}