Created
February 22, 2015 08:20
-
-
Save sathish-io/7ef2f38ad180b694192d to your computer and use it in GitHub Desktop.
Sample code to do HTTP Post using Apache HTTP Client Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import org.apache.commons.httpclient.Credentials; | |
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; | |
import org.apache.commons.httpclient.HttpClient; | |
import org.apache.commons.httpclient.HttpException; | |
import org.apache.commons.httpclient.HttpStatus; | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; | |
import org.apache.commons.httpclient.auth.AuthScope; | |
import org.apache.commons.httpclient.methods.PostMethod; | |
import org.apache.commons.httpclient.methods.StringRequestEntity; | |
import org.apache.commons.httpclient.params.HttpMethodParams; | |
public class HTTPClientDemo { | |
private static String url = "http://localhost:8080/apps/app1"; | |
public static void main(String[] args) { | |
// Create an instance of HttpClient. | |
HttpClient client = new HttpClient(); | |
Credentials defaultcreds = new UsernamePasswordCredentials("user", "password"); | |
client.getState().setCredentials(AuthScope.ANY, defaultcreds); | |
// Create a method instance. | |
PostMethod method = new PostMethod(url); | |
String body = getPostBody(); | |
StringRequestEntity requestEntity = null; | |
try { | |
requestEntity = new StringRequestEntity(body, "application/xml", "utf-8"); | |
} catch (UnsupportedEncodingException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
method.setRequestEntity(requestEntity); | |
// Provide custom retry handler is necessary | |
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, | |
new DefaultHttpMethodRetryHandler(3, false)); | |
try { | |
// Execute the method. | |
int statusCode = client.executeMethod(method); | |
if (statusCode != HttpStatus.SC_OK) { | |
System.err.println("Method failed: " + method.getStatusLine()); | |
} | |
// Read the response body. | |
byte[] responseBody = method.getResponseBody(); | |
// Deal with the response. | |
// Use caution: ensure correct character encoding and is not binary data | |
System.out.println(new String(responseBody)); | |
} catch (HttpException e) { | |
System.err.println("Fatal protocol violation: " + e.getMessage()); | |
e.printStackTrace(); | |
} catch (IOException e) { | |
System.err.println("Fatal transport error: " + e.getMessage()); | |
e.printStackTrace(); | |
} finally { | |
// Release the connection. | |
method.releaseConnection(); | |
} | |
} | |
public static String getPostBody() { | |
return "Sample POST Body"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment