您的位置:首页 > 理论基础 > 计算机网络

[Java语言] Java网络请求工具类

2016-04-19 17:53 716 查看
Java网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4) 

到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi 

import java.util.List;

import org.apache.http.HttpStatus;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;
http://www.nvzi91.cn/LEEP/30054.html
/**

* HttpServletUtil

*

* @author ysj

* @Date: 2015-1-30 下午2:07:55

*/

public class HttpServletUtil {

private static CloseableHttpClient httpclient;

private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();

/**

* Post:访问数据库并返回数据字符串

*http://www.nvzi91.cn/kunmingrenliu/041930055.html

* @param params

* 向服务器端传的参数

* @param url

* @return String 数据字符串

* @throws Exception

*/

public static String doPost(List<NameValuePair> params, String url) throws Exception {

String result = null;

httpclient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(url);

httpPost.setEntity(new UrlEncodedFormEntity(params));

//设置请求和传输超时时间

httpPost.setConfig(requestConfig);

CloseableHttpResponse httpResp = httpclient.execute(httpPost);

try {

int statusCode = httpResp.getStatusLine().getStatusCode();

// 判断是够请求成功

if (statusCode == HttpStatus.SC_OK) {

System.out.println("状态码:" + statusCode);

System.out.println("请求成功!");

// 获取返回的数据

result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");

} else {

System.out.println("状态码:"

+ httpResp.getStatusLine().getStatusCode());

System.out.println("HttpPost方式请求失败!");

}

} finally {

httpResp.close();

httpclient.close();

}http://www.nvzi91.cn/shengzhizhengxing/30056.html

return result;

}

/**

* Get:访问数据库并返回数据字符串

*

* @param url

* @return String 数据字符串

* @throws Exception

*/

public static String doGet(String url) throws Exception{

String result = null;

httpclient = HttpClients.createDefault();

HttpGet httpGet = new HttpGet(url);

//设置请求和传输超时时间

httpGet.setConfig(requestConfig);

CloseableHttpResponse httpResp = httpclient.execute(httpGet);

try {

int statusCode = httpResp.getStatusLine().getStatusCode();

// 判断是够请求成功

if (statusCode == HttpStatus.SC_OK) {

System.out.println("状态码:" + statusCode);

System.out.println("请求成功!");

// 获取返回的数据

result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");

} else {http://www.nvzi91.cn/luanchaonanzhong/30057.html

System.out.println("状态码:"

+ httpResp.getStatusLine().getStatusCode());

System.out.println("HttpGet方式请求失败!");

}http://www.nvzi91.cn/fujianyan/30058.html

} finally {

httpResp.close();

httpclient.close();

}www.nvzi91.cn

return result;

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: