在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ java發(fā)送http請(qǐng)求,無(wú)需等待返回結(jié)果

java發(fā)送http請(qǐng)求,無(wú)需等待返回結(jié)果

與B項(xiàng)目進(jìn)行交互,由于B項(xiàng)目一些原因,請(qǐng)求處理緩慢.A項(xiàng)目等待返回結(jié)果需要很久...
現(xiàn)只需發(fā)送數(shù)據(jù)無(wú)需判斷發(fā)送成功失敗與否,求教如何操作???
以下是發(fā)送get請(qǐng)求的代碼

    public static String doGet(String HTTP_URL, Object object) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer httpUrl = new StringBuffer(HTTP_URL);
        StringBuffer sbf = new StringBuffer();
        try {
            System.out.println(httpUrl.toString());
            URL url = new URL(httpUrl.toString());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 請(qǐng)求方式設(shè)置 POST
            connection.setRequestMethod("GET");
            // 設(shè)置維持長(zhǎng)連接
            connection.setRequestProperty("Connection", "Keep-Alive");
            // 設(shè)置文件字符集:
            connection.setRequestProperty("Charset", "UTF-8");
            // 開(kāi)始連接請(qǐng)求
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write((object.toString()).getBytes());
            out.flush();
            out.close();
            if (connection.getResponseCode() == 200) {
                System.out.println("連接成功,傳送數(shù)據(jù)...");
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("\r\n");
                }
                reader.close();
                result = sbf.toString();
                if (result.equals("1")) {
                    return "1";
                } else if(result.equals("0")) {
                    return "0";
                } else {
                    return result;
                }
            } else {
                System.out.println("連接失敗,錯(cuò)誤代碼:"+connection.getResponseCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
回答
編輯回答
避風(fēng)港

HttpURLConnection 有個(gè) setReadTimeout 的方法可以實(shí)現(xiàn)你的需求。

public static String doGet(String HTTP_URL, Object object) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer httpUrl = new StringBuffer(HTTP_URL);
        StringBuffer sbf = new StringBuffer();
        HttpURLConnection connection = null;
        try {
            System.out.println(httpUrl.toString());
            URL url = new URL(httpUrl.toString());
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 請(qǐng)求方式設(shè)置 POST
            connection.setRequestMethod("GET");
            // 設(shè)置維持長(zhǎng)連接
            connection.setRequestProperty("Connection", "Keep-Alive");
            // 設(shè)置文件字符集:
            connection.setRequestProperty("Charset", "UTF-8");

            //根據(jù)需求設(shè)置讀超時(shí)的時(shí)間
            connection.setReadTimeout(50);
            // 開(kāi)始連接請(qǐng)求
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write((object.toString()).getBytes());
            out.flush();
            out.close();
            if (connection.getResponseCode() == 200) {
                System.out.println("連接成功,傳送數(shù)據(jù)...");
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("\r\n");
                }
                reader.close();
                result = sbf.toString();
                if (result.equals("1")) {
                    return "1";
                } else if(result.equals("0")) {
                    return "0";
                } else {
                    return result;
                }
            } else {
                System.out.println("連接失敗,錯(cuò)誤代碼:" + connection.getResponseCode());
            }
        } catch (Exception e) {
            if(e instanceof SocketTimeoutException) {
                if("Read timed out".equals(e.getMessage()) && connection != null && connection.getDoOutput()) {
                    //TODO 只請(qǐng)求不需要響應(yīng)
                    return null;
                }
            }
            e.printStackTrace();
        }
        return null;
    }
2018年1月26日 19:52
編輯回答
壞脾滊

如果是使用spring,可以使用@Async來(lái)做異步。
如果不是可以自定義線程池,把發(fā)送請(qǐng)求的任務(wù)execute提交到線程池中進(jìn)行處理。


如果后面還想獲得該http請(qǐng)求的返回值,請(qǐng)使用submit提交任務(wù),會(huì)返回一個(gè)Future,future.get()會(huì)阻塞至直到獲得該任務(wù)的結(jié)果。

2018年4月15日 01:16
編輯回答
浪蕩不羈
 public static String doGet(String HTTP_URL, Object object) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer httpUrl = new StringBuffer(HTTP_URL);
        StringBuffer sbf = new StringBuffer();
        try {
            System.out.println(httpUrl.toString());
            URL url = new URL(httpUrl.toString());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 請(qǐng)求方式設(shè)置 POST
            connection.setRequestMethod("POST");
            // 設(shè)置文件字符集:
            connection.setRequestProperty("Charset", "UTF-8");
            // 開(kāi)始連接請(qǐng)求
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write((object.toString()).getBytes());
            out.flush();
            out.close();
            if (connection.getResponseCode() == 200) {
                System.out.println("連接成功,傳送數(shù)據(jù)...");
               
            } else {
                System.out.println("連接失敗,錯(cuò)誤代碼:"+connection.getResponseCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
2017年4月23日 04:17