标签:
1 package com.http.utils; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.UnsupportedEncodingException; 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.List; 10 import java.util.Map; 11 12 import org.apache.http.HttpResponse; 13 import org.apache.http.NameValuePair; 14 import org.apache.http.client.entity.UrlEncodedFormEntity; 15 import org.apache.http.client.methods.CloseableHttpResponse; 16 import org.apache.http.client.methods.HttpPost; 17 18 import org.apache.http.impl.client.CloseableHttpClient; 19 import org.apache.http.impl.client.HttpClients; 20 import org.apache.http.message.BasicNameValuePair; 21 22 23 public class Httpclient { 24 public static String sendHttpClientPost(String path,Map<String, String> map,String encode ) throws UnsupportedEncodingException{ 25 List<NameValuePair> list=new ArrayList<NameValuePair>(); 26 if(map!=null&&!map.isEmpty()){ 27 for(Map.Entry<String, String> entry:map.entrySet()){ 28 list.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); 29 } 30 } 31 CloseableHttpClient httpClient=null; 32 try { 33 //实现将请求的参数封装到表单中 34 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list, encode); 35 //以Post方式提交数据 36 HttpPost httpPost=new HttpPost(path); 37 //设置参数 38 httpPost.setEntity(entity); 39 //HttpClients工厂创建默认HttpClient实例 40 httpClient=HttpClients.createDefault(); 41 //public CloseableHttpResponse execute方法 42 //发送请求返回CloseableHttpResponse 43 HttpResponse httpResponse=httpClient.execute(httpPost); 44 System.out.println(httpResponse.getEntity().toString()); 45 //获取响应状态码 46 int responsecode=httpResponse.getStatusLine().getStatusCode(); 47 System.out.println(responsecode); 48 if(responsecode==200){ 49 return changeInputStream(httpResponse.getEntity().getContent(), encode); 50 } 51 } catch (IOException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 }finally { 55 try { 56 httpClient.close(); 57 } catch (IOException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 } 62 return null; 63 } 64 /** 65 * 强输入流转换为字符串输出到内存 66 * @param inputStream 67 * @param encode 68 * @return 69 */ 70 private static String changeInputStream(InputStream inputStream,String encode){ 71 ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); 72 byte[] buffer=new byte[1024]; 73 int length=0; 74 try { 75 while(-1!=(length=inputStream.read(buffer))){ 76 outputStream.write(buffer, 0, length); 77 } 78 return new String(outputStream.toByteArray(), encode); 79 } catch (IOException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 84 return null; 85 } 86 public static void main(String[] args) { 87 Map<String, String> parames=new HashMap<String,String>(); 88 parames.put("username", "admin"); 89 parames.put("password", "admin"); 90 String str = null; 91 try { 92 str = sendHttpClientPost("http://localhost:8080/login/loginServlet", parames, "utf-8"); 93 } catch (UnsupportedEncodingException e) { 94 // TODO Auto-generated catch block 95 e.printStackTrace(); 96 } 97 System.out.println(str); 98 } 99 }
标签:
原文地址:http://www.cnblogs.com/oldcownotGiveup/p/5405661.html