Advertisement
femencha

Sample http request.

Mar 29th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. public static void main(String[] args) {
  2.         System.out.println("Hello World let us make a sample request.!");
  3.        
  4.         String uri = "http://pehcs.invalidbyte.com/pehcs/pos/ws?" + "vi=2135&op=cv&connid=1490778490localhost0.77894629517472";
  5.         System.out.println("the response is this: " + sendGetRequest(uri)) ;
  6.  
  7.     }
  8.  
  9.     public static String sendGetRequest(String uri) {
  10.         try {
  11.             URL url = new URL(uri);
  12.             HttpURLConnection con = (HttpURLConnection) url.openConnection();
  13.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  14.  
  15.             String result;
  16.  
  17.             StringBuilder sb = new StringBuilder();
  18.  
  19.             while ((result = bufferedReader.readLine()) != null) {
  20.                 sb.append(result);
  21.             }
  22.  
  23.             return sb.toString();
  24.         } catch (Exception e) {
  25.             System.out.println("An error occured: " + e.getMessage());
  26.             e.printStackTrace();
  27.         }
  28.  
  29.         return null ;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement