1. 程式人生 > >使用java發送HTTP請求

使用java發送HTTP請求

catch msi static bsp nal string pro nbsp exceptio

 1 public class Test {
 2     public static void main(String[] args) {
 3         BufferedReader in = null;
 4         String result = "";
 5         try {
 6             String path = "http://127.0.0.1:6666/webapp/servlet/workItemServlet?method=gotWorkItem&dataTime=05-01";
 7             URL realUrl = new
URL(path); 8 URLConnection connection = realUrl.openConnection(); 9 connection.setRequestProperty("accept", "*/*"); 10 connection.setRequestProperty("connection", "Keep-Alive"); 11 connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
12 connection.connect(); 13 in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 14 String line; 15 while ((line = in.readLine()) != null) { 16 result += line; 17 } 18 List<Object> list = JsonUtil.toObjectList(result, Object.class
); 19 for (Object obj : list) { 20 System.out.println(obj.toString()); 21 } 22 } catch (Exception e) { 23 System.out.println("12345 workItem find error"); 24 } finally { 25 try { 26 if (in != null) { 27 in.close(); 28 } 29 } catch (Exception e2) { 30 e2.printStackTrace(); 31 } 32 } 33 } 34 }

使用java發送HTTP請求