[code]
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClientUtil {
public static final String CONN_HTTP="http", CONN_HTTPS="https";
/** Log object for this class. */
private static final Logger logger = LoggerFactory.getLogger( HttpClientUtil.class );
private static HttpClientUtil instance = new HttpClientUtil();
public static HttpClientUtil getInstance() {
return instance;
}
private HttpClientUtil() {
// HttpConnectionManagerParams httpConnMgrParams = new HttpConnectionManagerParams();
// httpConnMgrParams.setLinger( 0 );
// httpConnMgrParams.setTcpNoDelay( true );
httpConnMgrParams.setSendBufferSize( 1024 );
httpConnMgrParams.setReceiveBufferSize( 10240 );
httpConnMgrParams.setStaleCheckingEnabled( false );
// httpConnMgrParams.setMaxTotalConnections( 100 );
// httpConnMgrParams.setDefaultMaxConnectionsPerHost( 50 );
// httpConnMgrParams.setSoTimeout(6000);
// httpConnMgrParams.setConnectionTimeout(6000);
// mtHttpConnMgr.setParams( httpConnMgrParams );
}
public static String sendPostRequest(String url,String body) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity reqEntity = new StringEntity(body,"utf-8");
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new Exception( "Status Code is " + statusCode );
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return EntityUtils.toString(entity);
} else {
return "";
}
}
public String sendGetRequest(String url) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new Exception( "Status Code is " + statusCode );
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return EntityUtils.toString(entity);
} else {
return "";
}
}
public InputStream sendGetRequestFetchInputStream(String url) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.error( "Status Code is " + statusCode );
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return entity.getContent();
} else {
return null;
}
}
public static void main(String[] args) throws Exception {
HttpClientUtil hc = new HttpClientUtil();
String res = hc.sendGetRequest("http://www.baidu.com");
System.out.println("response:"+res);
}
}[/code]
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClientUtil {
public static final String CONN_HTTP="http", CONN_HTTPS="https";
/** Log object for this class. */
private static final Logger logger = LoggerFactory.getLogger( HttpClientUtil.class );
private static HttpClientUtil instance = new HttpClientUtil();
public static HttpClientUtil getInstance() {
return instance;
}
private HttpClientUtil() {
// HttpConnectionManagerParams httpConnMgrParams = new HttpConnectionManagerParams();
// httpConnMgrParams.setLinger( 0 );
// httpConnMgrParams.setTcpNoDelay( true );
httpConnMgrParams.setSendBufferSize( 1024 );
httpConnMgrParams.setReceiveBufferSize( 10240 );
httpConnMgrParams.setStaleCheckingEnabled( false );
// httpConnMgrParams.setMaxTotalConnections( 100 );
// httpConnMgrParams.setDefaultMaxConnectionsPerHost( 50 );
// httpConnMgrParams.setSoTimeout(6000);
// httpConnMgrParams.setConnectionTimeout(6000);
// mtHttpConnMgr.setParams( httpConnMgrParams );
}
public static String sendPostRequest(String url,String body) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity reqEntity = new StringEntity(body,"utf-8");
httppost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new Exception( "Status Code is " + statusCode );
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return EntityUtils.toString(entity);
} else {
return "";
}
}
public String sendGetRequest(String url) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new Exception( "Status Code is " + statusCode );
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return EntityUtils.toString(entity);
} else {
return "";
}
}
public InputStream sendGetRequestFetchInputStream(String url) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.error( "Status Code is " + statusCode );
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
return entity.getContent();
} else {
return null;
}
}
public static void main(String[] args) throws Exception {
HttpClientUtil hc = new HttpClientUtil();
String res = hc.sendGetRequest("http://www.baidu.com");
System.out.println("response:"+res);
}
}[/code]