中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java網(wǎng)絡(luò)請(qǐng)求工具類

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
Java網(wǎng)絡(luò)請(qǐng)求工具類(依賴:org.apache.http;注:HttpClient 4.4,HttpCore 4.4)
到此處可以去下載依賴包:http://hc.apache.org/downloads.cgi
import java.util.List;
 
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
/**
 * HttpServletUtil
 *
 * @author ysj
 * @Date: 2015-1-30 下午2:07:55
 */
public class HttpServletUtil {
    private static CloseableHttpClient httpclient;
    private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
 
    /**
     * Post:訪問(wèn)數(shù)據(jù)庫(kù)并返回?cái)?shù)據(jù)字符串
     *
     * @param params
     *            向服務(wù)器端傳的參數(shù)
     * @param url
     * @return String 數(shù)據(jù)字符串
     * @throws Exception
     */
    public static String doPost(List<NameValuePair> params, String url) throws Exception {
        String result = null;
        httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
        httpPost.setConfig(requestConfig);
        CloseableHttpResponse httpResp = httpclient.execute(httpPost);
        try {
            int statusCode = httpResp.getStatusLine().getStatusCode();
            // 判斷是夠請(qǐng)求成功
            if (statusCode == HttpStatus.SC_OK) {
                System.out.println("狀態(tài)碼:" + statusCode);
                System.out.println("請(qǐng)求成功!");
                // 獲取返回的數(shù)據(jù)
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
            } else {
                System.out.println("狀態(tài)碼:"
                        + httpResp.getStatusLine().getStatusCode());
                System.out.println("HttpPost方式請(qǐng)求失敗!");
            }
        } finally {
            httpResp.close();
            httpclient.close();
        }
        return result;
    }
 
    /**
     * Get:訪問(wèn)數(shù)據(jù)庫(kù)并返回?cái)?shù)據(jù)字符串
     *
     * @param url
     * @return String 數(shù)據(jù)字符串
     * @throws Exception
     */
     public static String doGet(String url) throws Exception{
        String result = null;
        httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        //設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
        httpGet.setConfig(requestConfig);
        CloseableHttpResponse httpResp = httpclient.execute(httpGet);
        try {
            int statusCode = httpResp.getStatusLine().getStatusCode();
            // 判斷是夠請(qǐng)求成功
            if (statusCode == HttpStatus.SC_OK) {
                System.out.println("狀態(tài)碼:" + statusCode);
                System.out.println("請(qǐng)求成功!");
                // 獲取返回的數(shù)據(jù)
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
            } else {
                System.out.println("狀態(tài)碼:"
                        + httpResp.getStatusLine().getStatusCode());
                System.out.println("HttpGet方式請(qǐng)求失敗!");
            }
        } finally {
            httpResp.close();
            httpclient.close();
        }
        return result;
     }
}

標(biāo)簽: 服務(wù)器 服務(wù)器端 數(shù)據(jù)庫(kù) 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php實(shí)現(xiàn)301永久重定向

下一篇:php使用ZipArchive壓縮打包文件