后台发送http请求

URLConnection urlCon = null;  
      OutputStreamWriter out = null;  
      BufferedReader reader = null;  
      InputStreamReader ir = null;  
      InputStream input = null;  
      OutputStream raw = null;  
      OutputStream buf = null;  
      String xmlString = "";  
  
       try  
       {  
           urlCon = new URL(urlStr).openConnection();  
  
           urlCon.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");  
           urlCon.setRequestProperty("Accept",  
                   "application/x-www-form-urlencoded");  
           urlCon.setRequestProperty("version", "100");  
           urlCon.setRequestProperty("Keep-Alive", "300");  
           urlCon.setRequestProperty("Content-Type",  
                   "application/x-www-form-urlencoded");  
           urlCon.setDoOutput(true);  
           urlCon.setDoInput(true);  
           urlCon.setReadTimeout(NUM_SIXTY_THOUSAND);  
  
           raw = urlCon.getOutputStream();  
           buf = new BufferedOutputStream(raw);  
           out = new OutputStreamWriter(buf, "UTF-8");  
  
           out.write(xmlMsg);  
           out.flush();  
           input = urlCon.getInputStream();  
           ir = new InputStreamReader(input, "UTF-8");  
           reader = new BufferedReader(ir);  
           String line = null;  
           StringBuffer xmlStr = new StringBuffer();  
           while ((line = reader.readLine()) != null)  
           {  
               xmlStr.append(line);  
           }  
  
           xmlString = xmlStr.toString().trim();  
       }  
      
       return xmlString;

 

转载于:https://my.oschina.net/exit/blog/155685

在Java后台(通常指服务器端)发送HTTP请求,可以使用多种库来处理,其中最常用的是`java.net`包下的`HttpURLConnection`或更现代的第三方库如`Apache HttpClient`、`OkHttp`或`Spring Web Client`。 1. 使用`HttpURLConnection`: ```java URL url = new URL("http://example.com/api"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); // 或POST、PUT等 connection.setDoOutput(true); // 如果有数据需要发送 try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } System.out.println(response.toString()); } // 关闭连接 connection.disconnect(); ``` 2. 使用`Apache HttpClient`: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://example.com/api"); try { HttpResponse response = httpClient.execute(httpGet); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { System.out.println(response.getStatusLine().toString()); try (BufferedReader in = new BufferedReader( new InputStreamReader(response.getEntity().getContent()))) { String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } System.out.println(content.toString()); } finally { response.close(); } } } catch (IOException e) { e.printStackTrace(); } finally { httpClient.close(); } ``` 3. 使用`Spring WebClient` (Spring Boot项目): ```java RestTemplate restTemplate = new RestTemplate(); // 如果配置了Spring自动注入,则不需要创建 ResponseEntity<String> response = restTemplate.getForEntity("http://example.com/api", String.class); String responseBody = response.getBody(); System.out.println(responseBody); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值