用Java模拟Http请求

1),利用httpclient4.× 写一个http的客户端,模拟浏览器请求, 
  1. public void post(List<NameValuePair> payload) throws  Exception{    
  2.         HttpPost post = new HttpPost(uri);    
  3.         HttpEntity result = null;    
  4.         try {    
  5.             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(payload,    
  6.                     charset);    
  7.             post.setEntity(entity);    
  8.             if (LOG.isDebugEnabled()) {    
  9.                 LOG.debug("sending:" + payload);    
  10.             }    
  11.     
  12.             HttpResponse response = _httpClient.execute(post);    
  13.             StatusLine statusLine = response.getStatusLine();    
  14.             if (statusLine.getStatusCode() != HttpStatus.SC_OK) {    
  15.                 result = response.getEntity();    
  16.                 StringBuilder msg = new StringBuilder();    
  17.                 msg.append("http response with code "    
  18.                         + statusLine.getStatusCode());    
  19.                 msg.append("\n");    
  20.                 msg.append("post request: " + post.getURI());    
  21.                 msg.append("\n");    
  22.                 msg.append(statusLine.getReasonPhrase());    
  23.                 if (result != null) {    
  24.                     msg.append("\n\n");    
  25.                     msg.append(EntityUtils.toString(result, "UTF-8"));    
  26.                     msg.append("\n\n");    
  27.                 }    
  28.                 throw new UmcException(msg.toString());    
  29.             }    
  30.             if (response.getEntity() != null) {    
  31.                 BufferedReader reader = new BufferedReader(    
  32.                         new InputStreamReader(    
  33.                                 response.getEntity().getContent(), "UTF-8"));    
  34.                 String line = null;    
  35.                 while ((line = reader.readLine()) != null) {    
  36.                     if (line.indexOf("success") < 0)    
  37.                         System.out.println(line);    
  38.                 }    
  39.             }    
  40.         } finally {    
  41.             if (result != null)    
  42.                 try {    
  43.                     EntityUtils.consume(result);    
  44.                 } catch (IOException e) {    
  45.                 }    
  46.             post.abort();    
  47.         }    
  48.     }    


uri是请求的地址,charset是编码“UTF-8”,List<NameValuePair>就是表单参数集 
Java代码   收藏代码
  1. ClientConnectionManager     ccManager = new ThreadSafeClientConnManager();    
  2. HttpClient      _httpClient = new DefaultHttpClient(ccManager);   


2) 采用JDK的HttpConnection构造http客户端, 
  1. ////发送    
  2.     HttpURLConnection conn = null;    
  3.     try {    
  4.         URL url = new URL(Your_URL);    
  5.         conn = (HttpURLConnection) url.openConnection();    
  6.         conn.setRequestMethod("POST");    
  7.         conn.setRequestProperty("Content-Type",    
  8.                 "application/x-www-form-urlencoded");    
  9.         conn.setUseCaches(false);    
  10.         conn.setDoOutput(true);    
  11.         OutputStreamWriter osw = new OutputStreamWriter(    
  12.                 conn.getOutputStream());    
  13.         StringBuffer sb = new StringBuffer();    
  14.         addPair(sb, "p1""p1value");    
  15.         addPair(sb, "p2""p2value");    
  16.         osw.write(sb.substring(0, sb.length() - 1));    
  17.         osw.flush();    
  18.         BufferedReader reader = new BufferedReader(    
  19.                 new InputStreamReader(conn.getInputStream()));    
  20.         String line = null;    
  21.         sb = new StringBuffer();    
  22.         while ((line = reader.readLine()) != null) {    
  23.             sb.append(line);    
  24.         }    
  25.         line = sb.toString();    
  26.         // 处理返回的字符串line    
  27.         return;    
  28.         ////    
  29.     } catch (IOException e) {    
  30.         // handle e    
  31.     } finally {    
  32.         if (conn != null)    
  33.             conn.disconnect();    
  34.     }///发送结束    

addPair方法: 
  1. public static void addPair(StringBuffer sb, String name, String value) {    
  2.     if (value == null) {    
  3.         return;    
  4.     }    
  5.     sb.append(name);    
  6.     sb.append("=");    
  7.     sb.append(value);    
  8.     sb.append("&");    
  9. }  

你可能感兴趣的:(java,jdk,浏览器,null,url)