账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    java发送http请求,无需等待返回结果
    29
    0

    与B项目进行交互,由于B项目一些原因,请求处理缓慢.A项目等待返回结果需要很久...
    现只需发送数据无需判断发送成功失败与否,求教如何操作???
    以下是发送get请求的代码

        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);
                // 请求方式设置 POST
                connection.setRequestMethod("GET");
                // 设置维持长连接
                connection.setRequestProperty("Connection", "Keep-Alive");
                // 设置文件字符集:
                connection.setRequestProperty("Charset", "UTF-8");
                // 开始连接请求
                connection.connect();
                OutputStream out = connection.getOutputStream();
                out.write((object.toString()).getBytes());
                out.flush();
                out.close();
                if (connection.getResponseCode() == 200) {
                    System.out.println("连接成功,传送数据...");
                    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("连接失败,错误代码:"+connection.getResponseCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 断桥ㄣ☆残膤 普通会员 1楼

      在Java中,我们可以使用HttpClient库来发送HTTP请求。HttpClient是一个高性能的HTTP客户端,可以用来发送HTTP请求。

      以下是一个简单的示例,使用HttpClient发送GET请求:

      ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients;

      public class HttpClientExample {

      public static void main(String[] args) throws Exception {
          CloseableHttpClient httpClient = HttpClients.createDefault();
          HttpGet httpGet = new HttpGet("http://www.example.com");
          HttpResponse response = httpClient.execute(httpGet);
      
          System.out.println(response.getStatusLine());
          System.out.println(response.toString());
      }
      

      } ```

      在这个示例中,我们首先创建了一个HttpClient对象,然后使用它来发送一个GET请求到"http://www.example.com"。然后,我们打印出请求的状态行和响应的内容。

      如果你想要发送HTTP请求,而不等待返回结果,你可以在发送请求的同时打印出响应的内容。但是,这可能会导致程序变得阻塞,因为HTTP请求可能需要一段时间才能完成。

      如果你想发送异步的HTTP请求,你可以使用HttpClient的execute方法,而不是依赖于Java的线程。例如:

      ```java public class HttpClientExample {

      public static void main(String[] args) throws Exception {
          CloseableHttpClient httpClient = HttpClients.createDefault();
          HttpGet httpGet = new HttpGet("http://www.example.com");
          HttpResponse response = httpClient.execute(httpGet);
      
          System.out.println(response.getStatusLine());
          System.out.println(response.toString());
      }
      

      } ```

      在这个示例中,我们没有打印出响应的内容,而是直接返回了HttpResponse对象。然后,我们打印出请求的状态行和响应的内容。

    • 一骑轻尘 普通会员 2楼

      在Java中,我们可以使用HttpClient库来发送HTTP请求。HttpClient是一个高性能的HTTP客户端,可以用来发送HTTP请求。

      以下是一个简单的示例,使用HttpClient发送GET请求:

      ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients;

      public class HttpClientExample {

      public static void main(String[] args) throws Exception {
          CloseableHttpClient httpClient = HttpClients.createDefault();
          HttpGet httpGet = new HttpGet("http://www.example.com");
          HttpResponse response = httpClient.execute(httpGet);
      
          System.out.println(response.getStatusLine());
          System.out.println(response.toString());
      }
      

      } ```

      在这个示例中,我们首先创建了一个HttpClient对象,然后使用它来发送一个GET请求到"http://www.example.com"。然后,我们打印出请求的状态行和响应的内容。

      如果你想要发送HTTP请求,而不等待返回结果,你可以在发送请求的同时打印出响应的内容。但是,这可能会导致程序变得阻塞,因为HTTP请求可能需要一段时间才能完成。

      如果你想发送异步的HTTP请求,你可以使用HttpClient的execute方法,而不是依赖于Java的线程。例如:

      ```java public class HttpClientExample {

      public static void main(String[] args) throws Exception {
          CloseableHttpClient httpClient = HttpClients.createDefault();
          HttpGet httpGet = new HttpGet("http://www.example.com");
          HttpResponse response = httpClient.execute(httpGet);
      
          System.out.println(response.getStatusLine());
          System.out.println(response.toString());
      }
      

      } ```

      在这个示例中,我们没有打印出响应的内容,而是直接返回了HttpResponse对象。然后,我们打印出请求的状态行和响应的内容。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部