cilent

private static final int REQUEST_TIMEOUT = 20*1000;//设置请求超时10秒钟
private static final int SO_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
{
HttpClient client = new DefaultHttpClient();//创建一个http客户端 .
HttpGet httpGet = new HttpGet(url);//创建一个GET请求

HttpResponse response = client.execute(httpGet);//向服务器发送请求并获取服务器返回的结果
getHttpClient(client);
HttpEntity entity = response.getEntity();
if (null != entity)
{
InputStream inputStream = entity.getContent(); //返回的结果可能放到InputStream,http Header中等。
value = ParseString.Reader(inputStream);//解析流
//Header[] headers = response.getAllHeaders();
return value;
}
}catch (Exception e){
e.printStackTrace();
}
return value;
}
public static void setValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void updateValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPut.setEntity(entity);
HttpResponse response = client.execute(httpPut);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void deleteVlaues(String url)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
HttpResponse response = client.execute(httpDelete);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void getHttpClient(HttpClient client)
{
HttpParams httpParams=client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, //REQUEST_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, //SO_TIMEOUT);
//HttpClient client = new DefaultHttpClient(httpParams);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值