Skip to content

Instantly share code, notes, and snippets.

@vinsim24
Created February 11, 2017 05:43
Show Gist options
  • Save vinsim24/af315bd06daa9e6e5d612fa0e1929564 to your computer and use it in GitHub Desktop.
Save vinsim24/af315bd06daa9e6e5d612fa0e1929564 to your computer and use it in GitHub Desktop.
Java:Utility method to make GET and POST calls.
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
public class HttpUtil {
private static Logger logger = Logger.getLogger(HttpUtil.class);
public static final int HTTP_CONNECTION_TIMEOUT = 30000;
public static String sendGetRequest(String requestURL,int connectTimeout) throws Exception
{
HttpConnectionManagerParams connectionParams=new HttpConnectionManagerParams();
connectionParams.setConnectionTimeout(connectTimeout);
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().setParams(connectionParams);
GetMethod getMethod = new GetMethod();
URI uri = new URI(requestURL, true);
logger.debug("requestURL "+uri);
getMethod.setURI(uri);
int httpStatus = httpClient.executeMethod(getMethod);
InputStream is = getMethod.getResponseBodyAsStream();
String response = IOUtils.toString(is);
logger.debug("Response("+httpStatus+")s");
if(httpStatus != HttpStatus.SC_OK)
{
throw new Exception(response);
}
return response;
}
public static PostMethod sendPostRequest(String requestURL, Map<String, String> headersMap, String postDataBody, int connectionTimeout) throws Exception
{
logger.debug("Entering sendPostRequest method");
logger.debug("requestURL::"+requestURL+"::connectionTimeout"+connectionTimeout);
PostMethod postMethod = new PostMethod(requestURL);
if(headersMap != null && headersMap.size() > 0)
{
logger.debug("headersMap::"+headersMap);
for(Map.Entry<String,String> entry: headersMap.entrySet())
{
postMethod.setRequestHeader(entry.getKey(), entry.getValue());
}
}
postMethod.setRequestBody(postDataBody);
HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
connectionManagerParams.setConnectionTimeout(connectionTimeout);
SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
connectionManager.setParams(connectionManagerParams);
HttpClient httpClient = new HttpClient(connectionManager);
httpClient.executeMethod(postMethod);
logger.debug("Exiting sendPostRequest method");
return postMethod;
}
public static Map<String,String> responseHeadersMap(PostMethod postMethod) throws Exception
{
Map<String,String> responseHeadersMap = new HashMap<String, String>();
if(postMethod != null)
{
Header[] responseHeaders = postMethod.getResponseHeaders();
if (responseHeaders != null)
{
for (Header responseHeader : responseHeaders)
{
responseHeadersMap.put(responseHeader.getName(), responseHeader.getValue());
}
}
}
logger.debug("responseHeadersMap::"+responseHeadersMap);
return responseHeadersMap;
}
public static String responseBody(PostMethod postMethod)
{
String response = null;
try
{
response = IOUtils.toString(postMethod.getResponseBodyAsStream());
}catch (Exception e)
{
logger.error("Error response from server");
logger.error(e);
}
logger.debug("Final response::"+response);
return response;
}
public static void main(String[] args){
String postUrl = "<some post url>";
logger.debug("postUrl::"+postUrl);
String contentType = "application/xml";
Map<String, String> headersMap = new HashMap<String, String>();
headersMap.put("Content-type",contentType);
String postDataBody = "<some post body>";
PostMethod postMethod;
try {
postMethod = sendPostRequest(postUrl, headersMap, postDataBody, HTTP_CONNECTION_TIMEOUT);
Map<String,String> responseHeadersMap = responseHeadersMap(postMethod);
String responseContentType = (responseHeadersMap.get("Content-Type") != null)?responseHeadersMap.get("Content-Type"):responseHeadersMap.get("Content-type");
logger.debug("responseContentType::"+responseContentType);
String response = responseBody(postMethod);
logger.debug("Post url response::"+response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment