日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

核心代碼:

import JAVA.util.Map;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.io.ByteArrayOutputStream;

import org.Apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;

/**
 * Post請求工具類
 */
public class PostUtil {
    public static final String UTF8 = "utf-8";
    private static final String CONTENT_TYPE = "Application/json";

    public static String send(String url, Map<String, String> headers, Object params) {
        try {
            if (params instanceof Map)
                return sendMap(url, headers, (Map<String, Object>)params);
            else
                return sendJson(url, headers, (String)params);
        } catch (Exception e){
            return null;
        }
    }

    public static String sendMap(String url, Map<String, String> headers, Map<String, Object> params) throws IOException {
        List<BasicNameValuePair> pairs = new ArrayList<>();
        for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
            String name = iterator.next();
            Object value = params.get(name);
            pairs.add(new BasicNameValuePair(name, String.valueOf(value)));
        }

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, UTF8);
        return doAction(url, headers, entity);
    }

    public static String sendJson(String url, Map<String, String> headers, String params) throws IOException {
        StringEntity entity = new StringEntity(params, UTF8);
        return doAction(url, headers, entity);
    }

    private static String doAction(String url, Map<String, String> headers, StringEntity body) throws IOException {
        HttpPost post = new HttpPost(url);
        post.setEntity(body);
        for (String key : headers.keySet())
            post.addHeader(key, headers.get(key));

        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();

        String result = null;
        if (entity != null)
            result = EntityUtils.toString(entity, UTF8);

        response.close();
        return result;
    }

    /**
     * 發(fā)送 POST JSON 請求
     */
    public static String postJson(String url, String data) {
        HttpClientBuilder builder = HttpClientBuilder.create();
        HttpUriRequest request = null;
        // POST請求
        HttpEntity entity = new StringEntity(data, UTF8);
        HttpPost post = new HttpPost(url);
        post.setEntity(entity);
        request = post;
        request.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
        try (CloseableHttpClient closeableHttpClient = builder.build()) {
            HttpResponse resp = closeableHttpClient.execute(request);
            InputStream respIs = resp.getEntity().getContent();
            byte[] respBytes = toByteArray(respIs);
            String result = new String(respBytes, Charset.forName(UTF8));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static byte[] toByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try {
            byte[] b = new byte[4096];
            boolean var3 = false;

            int n;
            while ((n = is.read(b)) != -1) {
                output.write(b, 0, n);
            }

            byte[] var4 = output.toByteArray();
            return var4;
        } finally {
            output.close();
        }
    }
}

 

路漫漫其修遠兮,吾將上下而求索

譯文:在追尋真理方面,前方的道路還很漫長,但我將百折不撓,不遺余力地去追求和探索。

 

如果您有什么好的想法與方法,歡迎在評論區(qū)留言,我們一起討論~

分享到:
標簽:Java Post
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定