现在的位置: 首页 > 综合 > 正文

HttpClient联网工具类

2017年10月14日 ⁄ 综合 ⁄ 共 822字 ⁄ 字号 评论关闭
public class HttpConnect {
	public static String getNews(String url,List<? extends NameValuePair> parameters) {
		StringBuilder sb = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpPost post=new HttpPost(url);
		
		HttpParams params = client.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 5000);
		HttpConnectionParams.setSoTimeout(params, 5000);

		try {
			UrlEncodedFormEntity sendDate=new UrlEncodedFormEntity(parameters);
			post.setEntity(sendDate);
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(entity.getContent(), "UTF-8"),
							8192);
					String line = null;
					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
					reader.close();
					return sb.toString();
				}
			}
			
		} catch (Exception e) {

		}

		return null;
	}
	

抱歉!评论已关闭.