首页 > 代码库 > HttpClient联网工具类

HttpClient联网工具类

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;
	}
	

HttpClient联网工具类