1 httpClient封装后使用,get和post方法
1 package util; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.util.ArrayList; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 11 import net.sf.json.JSONObject; 12 13 import org.apache.http.HttpEntity; 14 import org.apache.http.HttpResponse; 15 import org.apache.http.NameValuePair; 16 import org.apache.http.client.HttpClient; 17 import org.apache.http.client.entity.UrlEncodedFormEntity; 18 import org.apache.http.client.methods.HttpPost; 19 import org.apache.http.entity.StringEntity; 20 import org.apache.http.impl.client.DefaultHttpClient; 21 import org.apache.http.message.BasicNameValuePair; 22 import org.apache.http.protocol.HTTP; 23 24 /** 25 * @Title:TestHttpClient 26 * @Author Tony 27 * @Date: 2014年6月21日 下午3:29:37 28 * @Description: httpClient使用,1 发送post请求 2 发送get请求 29 * 30 */ 31 public class TestHttpClient { 32 33 34 /** 35 * @Title: methodPost 36 * @Description: httpclient方法中post提交数据的使用 37 * @param @return 38 * @param @throws Exception 39 * @return String 40 * @throws 41 */ 42 public static String methodPost() throws Exception { 43 DefaultHttpClient httpclient = new DefaultHttpClient(); 44 // // 代理的设置 45 // HttpHost proxy = new HttpHost("10.60.8.20", 8080); 46 // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 47 // proxy); 48 49 // 目标地址 50 HttpPost httppost = new HttpPost( 51 "http://localhost:8011/testServlet"); 52 System.out.println("请求: " + httppost.getRequestLine()); 53 54 // post 参数 传递 55 List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 56 nvps.add(new BasicNameValuePair("content", "11111111")); // 参数 57 nvps.add(new BasicNameValuePair("path", "D:/file")); // 参数 58 nvps.add(new BasicNameValuePair("name", "8")); // 参数 59 nvps.add(new BasicNameValuePair("age", "9")); // 参数 60 nvps.add(new BasicNameValuePair("username", "wzt")); // 参数 61 62 httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 设置参数给Post 63 64 // 执行 65 HttpResponse response = httpclient.execute(httppost); 66 HttpEntity entity = response.getEntity(); 67 System.out.println(response.getStatusLine()); 68 if (entity != null) { 69 System.out.println("Response content length: " 70 + entity.getContentLength()); 71 } 72 // 显示结果 73 BufferedReader reader = new BufferedReader(new InputStreamReader( 74 entity.getContent(), "UTF-8")); 75 76 String line = null; 77 while ((line = reader.readLine()) != null) { 78 System.out.println(line); 79 } 80 if (entity != null) { 81 entity.consumeContent(); 82 } 83 return null; 84 85 } 86 87 /** 88 * @Title: methodGet 89 * @Description: 以get方法提交数的使用 90 * @param @return 91 * @param @throws Exception 92 * @return String 93 * @throws 94 */ 95 public static String methodGet() throws Exception { 96 DefaultHttpClient httpclient = new DefaultHttpClient(); 97 // // 代理的设置 98 // HttpHost proxy = new HttpHost("10.60.8.20", 8080); 99 // httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 100 101 102 // 目标地址 103 HttpPost httpGet = new HttpPost("http://localhost:8011/testServlet"); 104 105 // 构造最简单的字符串数据 106 StringEntity reqEntity = new StringEntity("name=test&password=test"); 107 // 设置类型 108 reqEntity.setContentType("application/x-www-form-urlencoded"); 109 // 设置请求的数据 110 httpGet.setEntity(reqEntity); 111 112 // 执行 113 HttpResponse response = httpclient.execute(httpGet); 114 HttpEntity entity = response.getEntity(); 115 System.out.println(response.getStatusLine()); 116 117 if (entity != null) { 118 System.out.println("Response content length: " + entity.getContentLength()); //得到返回数据的长度 119 } 120 // 显示结果 121 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8")); 122 123 String line = null; 124 while ((line = reader.readLine()) != null) { 125 System.out.println(line); 126 } 127 if (entity != null) { 128 entity.consumeContent(); 129 } 130 return null; 131 132 } 133 134 /** 135 * 模拟url访问 从特定的url中获取json 136 * 137 * @param urlStr 138 * @param params 139 * @return json object ,or null if failed 140 * 参数经过封装后传过来 ,提交为 post请求 141 */ 142 private static JSONObject getJsonFromUrl(String urlStr, 143 Map<String, String> params) { 144 HttpClient httpClient = new DefaultHttpClient(); 145 HttpPost httpPost = new HttpPost(urlStr); 146 JSONObject json = null; 147 try { 148 if (params != null) { 149 Iterator<String> keys = params.keySet().iterator(); 150 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 151 while (keys.hasNext()) { 152 String key = keys.next(); 153 nvps.add(new BasicNameValuePair(key, params.get(key))); 154 } 155 httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 156 } 157 HttpResponse response = httpClient.execute(httpPost); 158 HttpEntity entity = response.getEntity(); 159 InputStream is = entity.getContent(); 160 byte[] bytes = new byte[256]; 161 StringBuffer sb = new StringBuffer(); 162 while (is.read(bytes) > 0) { 163 sb.append(new String(bytes)); 164 bytes = new byte[256]; 165 } 166 json = JSONObject.fromObject(sb.toString()); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 171 return json; 172 } 173 174 /** 175 * @Title: main 176 * @Description: 测试类 177 * @param @param args 178 * @return void 179 * @throws 180 */ 181 public static void main(String[] args) { 182 try { 183 TestHttpClient.methodGet(); 184 // TestHttpClient.methodPost(); 185 186 } catch (Exception e) { 187 // TODO Auto-generated catch block 188 e.printStackTrace(); 189 } 190 } 191 }
2 后台接受数据的servlet
1 public class TestServlet extends HttpServlet { 2 private static final long serialVersionUID = 1L; 3 4 5 /** 6 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) 7 */ 8 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 9 request.setCharacterEncoding( "utf-8"); 10 response.setContentType("text/html;charset=utf-8"); 11 response.setCharacterEncoding("utf-8"); 12 13 String name = request.getParameter("name"); 14 System.out.println("name 变量数据: "+name); 15 } 16 }