public class ClientRequestOperation {
private static final int REQUEST_TIMEOUT = 20 * 1000;// 设置请求超时10秒钟
private static final int SO_TIMEOUT = 20 * 1000; // 设置等待数据超时时间10秒钟
/**
*
* URL encode method
*
* @param URL
* @return
*/
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;
}
/**
*
* client get information from server by url
*
* @param url
* @return
*/
public static String getInfomations(String url)
{
String value = null;
try
{
/*
* 创建一个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);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return value;
private static final int REQUEST_TIMEOUT = 20 * 1000;// 设置请求超时10秒钟
private static final int SO_TIMEOUT = 20 * 1000; // 设置等待数据超时时间10秒钟
/**
*
* URL encode method
*
* @param URL
* @return
*/
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;
}
/**
*
* client get information from server by url
*
* @param url
* @return
*/
public static String getInfomations(String url)
{
String value = null;
try
{
/*
* 创建一个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);
}
}
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);
}
}