post请求测试代码

  1 public class TestResourceCommentRestController {
  2      private final static String API_URI_DEBUG = "http://localhost:8080/wodm-api/api/v1";
  3      public static final int HASH_INTERATIONS = 1024;
  4      private static boolean FLAG_DEBUG = true;
  5      
  6      public static void main(String[] args) {
  7            testComment();
  8 //        System.out.println("makeAppkey="+Strings.makeAppkey());
  9 //         makeAppkey();
 10 //         String[] chars = null;
 11 //         chars = Lists.newArrayList("").toArray(chars);
 12 //         for (int i = 0; i < 10; i++) {
 13 //              System.out.println("["+i+"]"+RandomStringUtils.random(32, "abcdefghijklmnopqrstuvwlyz0123456789"));
 14 //         }
 15            
 16      }
 17      
 18      public static void makeAppkey(){
 19            System.out.println(RandomStringUtils.random(32, "abcdefghijklmnopqrstuvwlyz0123456789"));
 20      }
 21      
 22      public static void testComment(){
 23            String httpUrl = null;
 24            
 25            if(FLAG_DEBUG){
 26                 httpUrl = API_URI_DEBUG + "/comment/reply";
 27            }else { httpUrl = API_URI + "/comment"; }
 28            String postBody = null;
 29            
 30            //ResourceCommentDTO1 dto = new ResourceCommentDTO1();
 31            JSONObject object=new JSONObject();
 32            for(long i = 584 ;i < 585 ; i++){
 33                 object.put("sendId", 449L);
 34                 object.put("content", "回复222222!");
 35                 object.put("receiveId", 457l);
 36                 object.put("parentId", 1877l);
 37                 object.put("commentId", 1877l);
 38                 object.put("type", 1);
 39                 object.put("resourceId",i);
 40                 
 41                 /*dto.setResourceId(i);
 42                 dto.setSendId(147L);
 43                 dto.setContent("回复222222!");
 44                 dto.setParentId(322l);*/
 45                 //List<Long> list=new ArrayList<Long>();
 46                 /*list.add(143l);
 47                 list.add(438l);*/
 48                 //dto.setReceiveIds(list);
 49                 //object.put("receiveIds", list);
 50                 postBody =  JSON.toJSONString(object);
 51                 //postBody = JSON.toJSONString(dto);
 52                 System.out.println("---"+postBody);
 53                 String respBody = httpPost(httpUrl, postBody);
 54                 System.out.println("respBody="+respBody);
 55            }
 56            
 57            
 58      }
 59      
 60      
 61      
 62      
 63      private static String httpPost(String httpUrl, String body) {
 64            URL remoteUrl = null;
 65            OutputStreamWriter wr = null;
 66            InputStream is = null;
 67            BufferedReader reader = null;
 68            try {
 69                 URLConnection conn = null;
 70                 remoteUrl = new URL(httpUrl);
 71                 conn = remoteUrl.openConnection();
 72                 conn.setDoOutput(true);
 73                 conn.setConnectTimeout(5 * 1000);// time out 5s
 74                 
 75                 conn.setRequestProperty("Content-Type", "application/json");
 76                 
 77 //              conn.setRequestProperty("Accept",
 78 //                        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
 79 //              
 80 //              conn.setRequestProperty("Accept",
 81 //                        "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
 82 //              conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
 83 //              conn.setRequestProperty("Connection", "keep-alive");
 84 //              conn.setRequestProperty(
 85 //                         "User-Agent",
 86 //                         "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:13.0) Gecko/20100101 Firefox/13.0.1");
 87                 wr = new OutputStreamWriter(conn.getOutputStream());
 88                 wr.write(body);
 89                 wr.flush();
 90                 is = conn.getInputStream();
 91                 reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
 92                 // response
 93                 StringBuffer responseBody = new StringBuffer();
 94                 String responseStr = null;
 95                 while ((responseStr = reader.readLine()) != null) {
 96                      if (responseBody.length() > 0) {
 97                            responseBody.append("
");
 98                      }
 99                      responseBody.append(responseStr);
100                 }
101                 return responseBody.toString();
102            } catch (MalformedURLException e) {
103                 e.printStackTrace();
104            } catch (UnsupportedEncodingException e) {
105                 e.printStackTrace();
106            } catch (IOException e) {
107                 e.printStackTrace();
108            } catch (Exception e) {
109                 e.printStackTrace();
110            }
111            return null;
112      }
113      
114 }
115   
原文地址:https://www.cnblogs.com/SimonHu1993/p/7103320.html