使用java发送HTTP请求
Posted yanwu0527
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java发送HTTP请求相关的知识,希望对你有一定的参考价值。
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请求的主要内容,如果未能解决你的问题,请参考以下文章