Java中的httpclient4.5应该咋使用
【Java中的httpclient4.5应该咋使用】 import java.io.IOException;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;@SuppressWarnings("deprecation")public class HttpUtil { private static final String UTF_8 = HTTP.UTF_8; public static String post(String url , Map\u0026lt;String , String\u0026gt; params) throws Exception{ DefaultHttpClient client = HttpFactory.createHttpClient(); HttpPost post = new HttpPost(url); if(params != null ){ List\u0026lt;BasicNameValuePair\u0026gt; lparams = new LinkedList\u0026lt;BasicNameValuePair\u0026gt;(); for (ConcurrentHashMap.Entry\u0026lt;String, String\u0026gt; entry : params.entrySet()) { if (entry.getValue() != null) { lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); }else{ lparams.add(new BasicNameValuePair(entry.getKey(), "")); } } HttpEntity entiry = new UrlEncodedFormEntity(lparams, UTF_8); post.setEntity(entiry); } try { HttpResponse resonse = client.execute(post); return entityToString(resonse); } catch (Exception exception) { throw exception; } finally { post.abort(); client.getConnectionManager().shutdown(); } } public static String get(String url) throws Exception{ DefaultHttpClient client = HttpFactory.createHttpClient(); HttpGet get = new HttpGet(url); try { HttpResponse resonse = client.execute(get); return entityToString(resonse); } catch (Exception exception) { throw exception; } finally { get.abort(); client.getConnectionManager().shutdown(); } } public static String entityToString(HttpResponse resonse) throws Exception{ HttpEntity entity = resonse.getEntity(); if (entity != null) { String msg = null; try { msg = EntityUtils.toString(entity, UTF_8); } catch (IOException e) { e.printStackTrace(); } int code = resonse.getStatusLine().getStatusCode(); if (code == 200) { return msg; } else { String errerMsg = (msg == null ? null : msg); throw new Exception("http code:" + code +",error:"+ errerMsg); } } throw new Exception("http entity is null"); } public static byte entityTobyte(HttpResponse resonse) throws Exception { HttpEntity entity = resonse.getEntity(); if (entity != null) { byte buffer = null; try { buffer = EntityUtils.toByteArray(entity); } catch (IOException e) { e.printStackTrace(); } int code = resonse.getStatusLine().getStatusCode(); if (code == 200) { return buffer; } else { String errerMsg = (buffer == null ? null : new String(buffer, UTF_8)); throw new Exception("http code:" + code +",error:"+ errerMsg); } } throw new Exception("http entity is null"); }}
推荐阅读
- 鄂温克冬季马赛-30℃极寒开赛:寒冬中的火热派对
- 大雪@大雪腌肉 适当进补 今日大雪
- |电商事业中的“闪光少年”
- dart这编程语言现在发展怎么样了,语法与Java,c#很相似,甚至更简洁
- hadoop中的mapreduce链接(mapreduce chaining)怎样避免中间文件的产生
- 经观汽车|日系车企中的“异类”?东风日产将导入e-POWER技术大干增程式混动 | 经观汽车
- 中年|这些东西,比你想象中的还要大得多!
- Java工程师和C++工程师在工作上有啥区别哪个更适合自身发展
- 请问杨毅微博中的这两人是谁
- 某些公司招聘要求中的精通mysql是啥程度