一个http请求处理的工具类

[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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值