HTTP Post Request using Apache Commons

Demonstrates an HTTP Post using the Apache Commons HTTP library.

Required Libraries:

Example Source Code

  import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair; public class ExampleHttpPost
{
public static void main(String args[]) throws ClientProtocolException, IOException
{
HttpPost httppost = new HttpPost("https://stanfordwho.stanford.edu/SWApp/Search.do"); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("search", "jsproch"));
parameters.add(new BasicNameValuePair("filters", "closed"));
parameters.add(new BasicNameValuePair("affilfilter", "everyone"));
parameters.add(new BasicNameValuePair("btnG", "Search")); httppost.setEntity(new UrlEncodedFormEntity(parameters)); HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity(); // Get the HTTP Status Code
int statusCode = httpResponse.getStatusLine().getStatusCode(); // Get the contents of the response
InputStream input = resEntity.getContent();
String responseBody = IOUtils.toString(input);
input.close(); // Print the response code and message body
System.out.println("HTTP Status Code: "+statusCode);
System.out.println(responseBody);
}
}
上一篇:报表或BI的价值在哪?


下一篇:【局部特征】ASIFT