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联网工具类
最新推荐文章于 2021-02-16 06:54:21 发布