001package org.kuali.ole.utility; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006import java.io.BufferedReader; 007import java.io.InputStreamReader; 008import java.io.OutputStreamWriter; 009import java.io.Writer; 010import java.net.URL; 011import java.net.URLConnection; 012 013/** 014 * Created by IntelliJ IDEA. 015 * User: ND6967 016 * Date: 12/21/11 017 * Time: 5:17 PM 018 * To change this template use File | Settings | File Templates. 019 */ 020public class HttpUtil { 021 022 private static final Logger LOG = LoggerFactory.getLogger(HttpUtil.class); 023 024 public static String postData(String target, String content) 025 throws Exception { 026 027 String response = ""; 028 URL url = new URL(target); 029 URLConnection conn = url.openConnection(); 030 conn.setDoInput(true); 031 conn.setDoOutput(true); 032 conn.setUseCaches(false); 033 conn.setRequestProperty("Content-Type", 034 "application/x-www-form-urlencoded"); 035 036 Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); 037 w.write(content); 038 w.close(); 039 BufferedReader in = new BufferedReader(new InputStreamReader( 040 conn.getInputStream())); 041 String temp; 042 while ((temp = in.readLine()) != null) { 043 response += temp + "\n"; 044 } 045 temp = null; 046 in.close(); 047 LOG.debug("Server response: " + response); 048 return response; 049 050 } 051 052}