View Javadoc
1   package org.kuali.ole.utility;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import java.io.BufferedReader;
7   import java.io.InputStreamReader;
8   import java.io.OutputStreamWriter;
9   import java.io.Writer;
10  import java.net.URL;
11  import java.net.URLConnection;
12  
13  /**
14   * Created by IntelliJ IDEA.
15   * User: ND6967
16   * Date: 12/21/11
17   * Time: 5:17 PM
18   * To change this template use File | Settings | File Templates.
19   */
20  public class HttpUtil {
21  
22      private static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class);
23  
24      public static String postData(String target, String content)
25              throws Exception {
26  
27          String response = "";
28          URL url = new URL(target);
29          URLConnection conn = url.openConnection();
30          conn.setDoInput(true);
31          conn.setDoOutput(true);
32          conn.setUseCaches(false);
33          conn.setRequestProperty("Content-Type",
34                  "application/x-www-form-urlencoded");
35  
36          Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
37          w.write(content);
38          w.close();
39          BufferedReader in = new BufferedReader(new InputStreamReader(
40                  conn.getInputStream()));
41          String temp;
42          while ((temp = in.readLine()) != null) {
43              response += temp + "\n";
44          }
45          temp = null;
46          in.close();
47          LOG.debug("Server response: " + response);
48          return response;
49  
50      }
51  
52  }